diff --git a/dataset_create.ipynb b/dataset_create.ipynb new file mode 100644 index 0000000..3faed21 --- /dev/null +++ b/dataset_create.ipynb @@ -0,0 +1,432 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Run cell only once, packages are specific to application\n", + "# !pip install selectivesearch\n", + "# !pip install colored-traceback\n", + "# !pip install scikit-image==0.15\n", + "# !pip install numpy==1.16" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from config import *\n", + "from selective_search import selective_search_bbox" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Global Variables\n", + "max_categories=50" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def create_dataset_split_structure():\n", + " if not os.path.exists(dataset_path):\n", + " os.makedirs(dataset_path)\n", + "\n", + " if not os.path.exists(dataset_train_path):\n", + " os.makedirs(dataset_train_path)\n", + "\n", + " if not os.path.exists(dataset_val_path):\n", + " os.makedirs(dataset_val_path)\n", + "\n", + " if not os.path.exists(dataset_test_path):\n", + " os.makedirs(dataset_test_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_dataset_split_name(image_path_name, file_ptr):\n", + " for line in file_ptr:\n", + " if image_path_name in line:\n", + " dataset_split_name=line.split()[1]\n", + " logging.debug('dataset_split_name {}'.format(dataset_split_name))\n", + " return dataset_split_name.strip()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_gt_bbox(image_path_name, file_ptr):\n", + " for line in file_ptr:\n", + " if image_path_name in line:\n", + " x1=int(line.split()[1])\n", + " y1=int(line.split()[2])\n", + " x2=int(line.split()[3])\n", + " y2=int(line.split()[4])\n", + " bbox = [x1, y1, x2, y2]\n", + " logging.debug('bbox {}'.format(bbox))\n", + " return bbox" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Get category names list\n", + "def get_category_names():\n", + " category_names = []\n", + " with open(fashion_dataset_path + '/Anno/list_category_cloth.txt') as file_list_category_cloth:\n", + " next(file_list_category_cloth)\n", + " next(file_list_category_cloth)\n", + " for line in file_list_category_cloth:\n", + " word=line.strip()[:-1].strip().replace(' ', '_')\n", + " category_names.append(word)\n", + " return category_names" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def create_category_structure(category_names):\n", + "\n", + " for idx,category_name in enumerate(category_names):\n", + "\n", + " if category_name not in category_name_generate:\n", + " logging.debug('Skipping category_names {}'.format(category_name))\n", + " continue\n", + "\n", + " logging.debug('category_names {}'.format(category_name))\n", + "\n", + " if idx < max_categories:\n", + " # Train\n", + " category_path_name=os.path.join(dataset_train_path, category_name)\n", + " logging.debug('category_path_name {}'.format(category_path_name))\n", + " if not os.path.exists(os.path.join(category_path_name)):\n", + " os.makedirs(category_path_name)\n", + "\n", + " # Validation\n", + " category_path_name=os.path.join(dataset_val_path, category_name)\n", + " logging.debug('category_path_name {}'.format(category_path_name))\n", + " if not os.path.exists(os.path.join(category_path_name)):\n", + " os.makedirs(category_path_name)\n", + "\n", + " # Test\n", + " category_path_name=os.path.join(dataset_test_path, category_name)\n", + " logging.debug('category_path_name {}'.format(category_path_name))\n", + " if not os.path.exists(os.path.join(category_path_name)):\n", + " os.makedirs(category_path_name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def bb_intersection_over_union(boxA, boxB):\n", + " # determine the (x, y)-coordinates of the intersection rectangle\n", + " xA = max(boxA[0], boxB[0])\n", + " yA = max(boxA[1], boxB[1])\n", + " xB = min(boxA[2], boxB[2])\n", + " yB = min(boxA[3], boxB[3])\n", + "\n", + " # compute the area of intersection rectangle\n", + " interArea = (xB - xA + 1) * (yB - yA + 1)\n", + "\n", + " # Added due to comments on page\n", + " if interArea < 0:\n", + " interArea = 0\n", + "\n", + " # compute the area of both the prediction and ground-truth rectangles\n", + " boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)\n", + " boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)\n", + "\n", + " # compute the intersection over union by taking the intersection\n", + " # area and dividing it by the sum of prediction + ground-truth\n", + " # areas - the interesection area\n", + " iou = interArea / float(boxAArea + boxBArea - interArea)\n", + "\n", + " # return the intersection over union value\n", + " return iou" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def display_bbox(image_path_name, boxA, boxB):\n", + " logging.debug('image_path_name {}'.format(image_path_name))\n", + "\n", + " # load image\n", + " img = skimage.io.imread(image_path_name)\n", + " logging.debug('img {}'.format(type(img)))\n", + "\n", + " # Draw rectangles on the original image\n", + " fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))\n", + " ax.imshow(img)\n", + "\n", + " # The origin is at top-left corner\n", + " x, y, w, h = boxA[0], boxA[1], boxA[2]-boxA[0], boxA[3]-boxA[1]\n", + " rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='green', linewidth=1)\n", + " ax.add_patch(rect)\n", + " logging.debug('GT: boxA {}'.format(boxA))\n", + " logging.debug(' x y w h')\n", + " logging.debug('{:4d} {:4d} {:4d} {:4d}'.format(x, y, w, h))\n", + "\n", + " x, y, w, h = boxB[0], boxB[1], boxB[2]-boxB[0], boxB[3]-boxB[1]\n", + " rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='red', linewidth=1)\n", + " ax.add_patch(rect)\n", + " logging.debug('boxB {}'.format(boxB))\n", + " logging.debug(' x y w h')\n", + " logging.debug('{:4d} {:4d} {:4d} {:4d}'.format(x, y, w, h))\n", + " \n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_bbox_score_and_save_img(image_path_name, dataset_image_path, gt_x1, gt_y1, gt_x2, gt_y2):\n", + "\n", + " logging.debug('dataset_image_path {}'.format(dataset_image_path))\n", + " logging.debug('image_path_name {}'.format(image_path_name))\n", + "\n", + " candidates = selective_search_bbox(image_path_name)\n", + " logging.debug('candidates {}'.format(candidates))\n", + "\n", + " image_name = image_path_name.split('/')[-1].split('.')[0]\n", + " logging.debug('image_name {}'.format(image_name))\n", + "\n", + " img_read = Image.open(image_path_name)\n", + " logging.debug('{} {} {}'.format(img_read.format, img_read.size, img_read.mode))\n", + "\n", + " i=0\n", + " for x, y, w, h in (candidates):\n", + " # left, upper, right, and lower pixel; The cropped section includes the left column and\n", + " # the upper row of pixels and goes up to (but doesn't include) the right column and bottom row of pixels\n", + " logging.debug('Cropped image: i {}'.format(i))\n", + " i=i+1\n", + "\n", + " boxA = (gt_x1, gt_y1, gt_x2, gt_y2)\n", + " boxB = (x, y, x+w, y+h)\n", + " iou = bb_intersection_over_union(boxA, boxB)\n", + " logging.debug('boxA {}'.format(boxA))\n", + " logging.debug('boxB {}'.format(boxB))\n", + " logging.debug('iou {}'.format(iou))\n", + "\n", + " # Uncomment only for testing as too much cpu/memory wastage\n", + " #display_bbox(image_path_name, boxA, boxB)\n", + "\n", + " #img_crop = img_read.crop((y, x, y+w, x+h))\n", + " img_crop = img_read.crop((x, y, x+w, y+h))\n", + "\n", + " image_save_name = image_path_name.split('/')[-2] + '_' + image_path_name.split('/')[-1].split('.')[0]\n", + " image_save_path = dataset_image_path.rsplit('/', 1)[0]\n", + " image_save_path_name = image_save_path + '/' + image_save_name + '_crop_' + str(x) + '-' + str(y) + '-' + str(x+w) + '-' + str(y+h) + '_iou_' + str(iou) + '.jpg'\n", + " logging.debug('image_save_path_name {}'.format(image_save_path_name))\n", + " img_crop.save(image_save_path_name)\n", + " logging.debug('img_crop {} {} {}'.format(img_crop.format, img_crop.size, img_crop.mode))\n", + "\n", + " # img_crop_resize = img_crop.resize((img_width, img_height))\n", + " # img_crop_resize.save('temp/test/'+ image_name + '_' + str(i) + '_cropped_resize' + '.jpg')\n", + " # logging.debug('img_crop_resize {} {} {}'.format(img_crop_resize.format, img_crop_resize.size, img_crop_resize.mode))\n", + "\n", + "\n", + " # Ground Truth\n", + " image_save_name = image_path_name.split('/')[-2] + '_' + image_path_name.split('/')[-1].split('.')[0]\n", + " image_save_path = dataset_image_path.rsplit('/', 1)[0]\n", + " image_save_path_name = image_save_path + '/' + image_save_name + '_gt_' + str(gt_x1) + '-' + str(gt_y1) + '-' + str(gt_x2) + '-' + str(gt_y2) + '_iou_' + '1.0' + '.jpg'\n", + " logging.debug('image_save_path_name {}'.format(image_save_path_name))\n", + " #img_crop = img_read.crop((gt_y1, gt_x1, gt_y2, gt_x2))\n", + " img_crop = img_read.crop((gt_x1, gt_y1, gt_x2, gt_y2))\n", + " img_crop.save(image_save_path_name)\n", + " logging.debug('img_crop {} {} {}'.format(img_crop.format, img_crop.size, img_crop.mode))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Generate images from fashon-data into dataset\n", + "def generate_dataset_images(category_names):\n", + "\n", + "\n", + " count=0\n", + " with open(fashion_dataset_path + '/Anno/list_bbox.txt') as file_list_bbox_ptr:\n", + " with open(fashion_dataset_path + '/Anno/list_category_img.txt') as file_list_category_img:\n", + " with open(fashion_dataset_path + '/Eval/list_eval_partition.txt', 'r') as file_list_eval_ptr:\n", + "\n", + " next(file_list_category_img)\n", + " next(file_list_category_img)\n", + " idx_crop=1\n", + " for line in file_list_category_img:\n", + " line = line.split()\n", + " image_path_name = line[0]\n", + " logging.debug('image_path_name {}'.format(image_path_name)) # img/Tailored_Woven_Blazer/img_00000051.jpg\n", + " image_name = line[0].split('/')[-1]\n", + " logging.debug('image_name {}'.format(image_name)) # image_name img_00000051.jpg\n", + " image_full_name = line[0].replace('/', '_')\n", + " logging.debug('image_full_name {}'.format(image_full_name)) # img_Tailored_Woven_Blazer_img_00000051.jpg\n", + " image_category_index=int(line[1:][0]) - 1\n", + " logging.debug('image_category_index {}'.format(image_category_index)) # 2\n", + "\n", + " if category_names[image_category_index] not in category_name_generate:\n", + " logging.debug('Skipping {} {}'.format(category_names[image_category_index], image_path_name))\n", + " continue\n", + "\n", + "\n", + " if image_category_index < max_categories:\n", + "\n", + " dataset_image_path = ''\n", + " dataset_split_name = get_dataset_split_name(image_path_name, file_list_eval_ptr)\n", + "\n", + " if dataset_split_name == \"train\":\n", + " dataset_image_path = os.path.join(dataset_train_path, category_names[image_category_index], image_full_name)\n", + " elif dataset_split_name == \"val\":\n", + " dataset_image_path = os.path.join(dataset_val_path, category_names[image_category_index], image_full_name)\n", + " elif dataset_split_name == \"test\":\n", + " dataset_image_path = os.path.join(dataset_test_path, category_names[image_category_index], image_full_name)\n", + " else:\n", + " logging.error('Unknown dataset_split_name {}'.format(dataset_image_path))\n", + " exit(1)\n", + "\n", + " logging.debug('image_category_index {}'.format(image_category_index))\n", + " logging.debug('category_names {}'.format(category_names[image_category_index]))\n", + " logging.debug('dataset_image_path {}'.format(dataset_image_path))\n", + "\n", + " # Get ground-truth bounding boxes\n", + " gt_x1, gt_y1, gt_x2, gt_y2 = get_gt_bbox(image_path_name, file_list_bbox_ptr) # Origin is top left, x1 is distance from y axis;\n", + " # x1,y1: top left coordinate of crop; x2,y2: bottom right coordinate of crop\n", + " logging.debug('Ground bbox: gt_x1:{} gt_y1:{} gt_x2:{} gt_y2:{}'.format(gt_x1, gt_y1, gt_x2, gt_y2))\n", + "\n", + " image_path_name_src = os.path.join(fashion_dataset_path, 'Img', image_path_name)\n", + " logging.debug('image_path_name_src {}'.format(image_path_name_src))\n", + "\n", + " calculate_bbox_score_and_save_img(image_path_name_src, dataset_image_path, gt_x1, gt_y1, gt_x2, gt_y2)\n", + "\n", + " #TODO: Also cropping in test set. Check if required\n", + " #shutil.copyfile(os.path.join(fashion_dataset_path, 'Img', image_path_name), dataset_image_path)\n", + "\n", + " idx_crop = idx_crop + 1\n", + " logging.debug('idx_crop {}'.format(idx_crop))\n", + "\n", + " # if idx_crop is 1000:\n", + " # exit(0)\n", + "\n", + " count = count+1\n", + " logging.info('count {} {}'.format(count, dataset_image_path))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Display category and images count\n", + "def display_category_data():\n", + " for path in [dataset_train_path, dataset_val_path, dataset_test_path]:\n", + " logging.info('path {}'.format(path))\n", + " path1, dirs1, files1 = next(os.walk(path))\n", + " file_count1 = len(files1)\n", + " for dirs1_name in dirs1:\n", + " path2, dirs2, files2 = next(os.walk(os.path.join(path, dirs1_name)))\n", + " file_count2 = len(files2)\n", + " logging.info('{:20s} : {}'.format(dirs1_name, file_count2))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create Structure\n", + "create_dataset_split_structure()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "category_names = get_category_names()\n", + "category_name_generate = category_names" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create Structure\n", + "create_category_structure(category_names)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "generate_dataset_images(category_names)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display_category_data()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:tensorflow_p36]", + "language": "python", + "name": "conda-env-tensorflow_p36-py" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/fashion_train_alt.ipynb b/fashion_train_alt.ipynb new file mode 100644 index 0000000..0fca8eb --- /dev/null +++ b/fashion_train_alt.ipynb @@ -0,0 +1,354 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import Libraries\n", + "from __future__ import print_function\n", + "\n", + "from config import *\n", + "from utils import *\n", + "from model import *" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Global Variables\n", + "class_names=[]\n", + "batch_size = 0\n", + "input_shape=(0,0,0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def init():\n", + "\n", + " global batch_size\n", + " batch_size = batch_size_train\n", + " logging.debug('batch_size {}'.format(batch_size))\n", + "\n", + " global class_names\n", + " class_names = sorted(get_subdir_list(dataset_train_path))\n", + " logging.debug('class_names {}'.format(class_names))\n", + "\n", + " global input_shape\n", + " input_shape = (img_width, img_height, img_channel)\n", + " logging.debug('input_shape {}'.format(input_shape))\n", + "\n", + "\n", + " if not os.path.exists(output_path_name):\n", + " os.makedirs(output_path_name)\n", + "\n", + " if not os.path.exists(logs_path_name):\n", + " os.makedirs(logs_path_name)\n", + "\n", + " if not os.path.exists(btl_path):\n", + " os.makedirs(btl_path)\n", + "\n", + " if not os.path.exists(btl_train_path):\n", + " os.makedirs(btl_train_path)\n", + "\n", + " if not os.path.exists(btl_val_path):\n", + " os.makedirs(btl_val_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def save_bottleneck():\n", + " logging.debug('class_names {}'.format(class_names))\n", + " logging.debug('batch_size {}'.format(batch_size))\n", + " logging.debug('epochs {}'.format(epochs))\n", + " logging.debug('input_shape {}'.format(input_shape))\n", + "\n", + "\n", + " ## Build the VGG16 network\n", + " model = applications.VGG16(include_top=False, weights='imagenet', input_shape=input_shape)\n", + " #model = applications.inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=input_shape)\n", + "\n", + "\n", + " for train_val in ['train', 'validation']:\n", + "\n", + " with open('bottleneck/btl_' + train_val + '.txt', 'w') as f_image:\n", + " for class_name in class_names:\n", + " dataset_train_class_path = os.path.join(dataset_path, train_val, class_name)\n", + " logging.debug('dataset_train_class_path {}'.format(dataset_train_class_path))\n", + "\n", + " images_list = []\n", + " images_name_list = []\n", + "\n", + " images_path_name = sorted(glob.glob(dataset_train_class_path + '/*.jpg'))\n", + " logging.debug('images_path_name {}'.format(len(images_path_name)))\n", + "\n", + " for index, image in enumerate(images_path_name):\n", + " # logging.debug('image {}'.format(image))\n", + "\n", + " img = Image.open(image)\n", + "\n", + " img = preprocess_image(img)\n", + "\n", + " current_batch_size = len(images_list)\n", + " # logging.debug('current_batch_size {}'.format(current_batch_size))\n", + "\n", + " images_list.append(img)\n", + "\n", + " image_name = image.split('/')[-1].split('.jpg')[0]\n", + " images_name_list.append(image)\n", + " images_list_arr = np.array(images_list)\n", + "\n", + " # TODO: Skipping n last images of a class which do not sum up to batch_size\n", + " if (current_batch_size < batch_size-1):\n", + " continue\n", + "\n", + "\n", + " X = images_list_arr\n", + "\n", + " bottleneck_features_train_class = model.predict(X, batch_size)\n", + " # bottleneck_features_train_class = model.predict(X, nb_train_class_samples // batch_size)\n", + "\n", + "\n", + " ## Save bottleneck file\n", + " btl_save_file_name = btl_path + train_val + '/btl_' + train_val + '_' + class_name + '.' + str(index).zfill(7) + '.npy'\n", + " logging.info('btl_save_file_name {}'.format(btl_save_file_name))\n", + " np.save(open(btl_save_file_name, 'wb'), bottleneck_features_train_class)\n", + " for name in images_name_list:\n", + " f_image.write(str(name) + '\\n')\n", + "\n", + " images_list = []\n", + " images_name_list = []" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def train_model():\n", + "\n", + " ## Build network\n", + " model = applications.VGG16(include_top=False, weights='imagenet', input_shape=input_shape)\n", + " #model = applications.inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=input_shape)\n", + "\n", + "\n", + "\n", + " # Get sorted bottleneck file names in a list\n", + " btl_train_names = sorted(glob.glob(btl_train_path + '/*.npy'))\n", + " btl_val_names = sorted(glob.glob(btl_val_path + '/*.npy'))\n", + "\n", + "\n", + "\n", + " ## Train Labels\n", + " btl_train_list = []\n", + " train_labels_class = []\n", + " train_labels_iou = []\n", + "\n", + " # Get list of image IoU values\n", + " with open('bottleneck/btl_train.txt') as f_btl_train:\n", + " btl_train_list = f_btl_train.readlines()\n", + " # logging.debug('btl_train_list {}'.format(btl_train_list))\n", + " for btl_train_image in btl_train_list:\n", + " train_labels_class.append(btl_train_image.split('/')[2])\n", + " iou_value = np.round(np.float( btl_train_image.split('_')[-1].split('.jpg')[0] ), 2)\n", + " train_labels_iou.append(iou_value)\n", + " # logging.debug('val {}'.format(val))\n", + "\n", + " # logging.debug('class_names {}'.format(class_names))\n", + " # logging.debug('train_labels_class {}'.format(train_labels_class))\n", + " train_labels_class_int = []\n", + " for index, class_name in enumerate(train_labels_class):\n", + " train_labels_class_int.append(class_names.index(class_name))\n", + " train_labels_class = train_labels_class_int\n", + " # logging.debug('train_labels_class {}'.format(train_labels_class))\n", + "\n", + " train_labels_class = np.array(train_labels_class)\n", + " train_labels_iou = np.array(train_labels_iou)\n", + " logging.debug('train_labels_iou {}'.format(train_labels_iou))\n", + " logging.debug('train_labels_iou {}'.format(type(train_labels_iou)))\n", + " logging.debug('train_labels_class {}'.format(type(train_labels_class)))\n", + " logging.debug('train_labels_class {}'.format((train_labels_class.shape)))\n", + "\n", + " # Load bottleneck files to create train set\n", + " train_data = []\n", + " for index, btl_name in enumerate(btl_train_names):\n", + " temp = np.load(open(btl_name))\n", + " train_data.append(temp)\n", + "\n", + " train_data = np.array(train_data)\n", + " n1, n2, w, h, c = train_data.shape\n", + " logging.info('train_data {}'.format(train_data.shape))\n", + " train_data_ = train_data\n", + " train_data = np.reshape(train_data_, (n1*n2, w, h, c))\n", + " logging.info('train_data {}'.format(train_data.shape))\n", + "\n", + "\n", + "\n", + " ## Validation Labels\n", + " btl_val_list = []\n", + " val_labels_class = []\n", + " val_labels_iou = []\n", + "\n", + " # Get list of image IoU values\n", + " with open('bottleneck/btl_validation.txt') as f_btl_val:\n", + " btl_val_list = f_btl_val.readlines()\n", + " # logging.debug('btl_val_list {}'.format(btl_val_list))\n", + " for btl_val_image in btl_val_list:\n", + " val_labels_class.append(btl_val_image.split('/')[2])\n", + " val = np.round(np.float( btl_val_image.split('_')[-1].split('.jpg')[0] ), 2)\n", + " val_labels_iou.append(val)\n", + " # logging.debug('val {}'.format(val))\n", + "\n", + " # logging.debug('val_labels_class {}'.format(val_labels_class))\n", + " val_labels_class_int = []\n", + " for index, class_name in enumerate(val_labels_class):\n", + " val_labels_class_int.append(class_names.index(class_name))\n", + " val_labels_class = val_labels_class_int\n", + " # logging.debug('val_labels_class {}'.format(val_labels_class))\n", + "\n", + " val_labels_class = np.array(val_labels_class)\n", + " # logging.debug('val_labels_class {}'.format(val_labels_class))\n", + " val_labels_iou = np.array(val_labels_iou)\n", + " # logging.debug('val_labels_iou {}'.format(val_labels_iou))\n", + " logging.debug('val_labels_iou {}'.format(type(val_labels_iou)))\n", + " logging.debug('val_labels_class {}'.format(type(val_labels_class)))\n", + " logging.debug('val_labels_class {}'.format(val_labels_class.shape))\n", + "\n", + " # Load bottleneck files to create validation set\n", + " val_data = []\n", + " for index, btl_name in enumerate(btl_val_names):\n", + " temp = np.load(open(btl_name,'rb'))\n", + " val_data.append(temp)\n", + "\n", + " val_data = np.array(val_data)\n", + " n1, n2, w, h, c = val_data.shape\n", + " logging.info('val_data {}'.format(val_data.shape))\n", + " val_data_ = val_data\n", + " val_data = np.reshape(val_data_, (n1*n2, w, h, c))\n", + " logging.info('val_data {}'.format(val_data.shape))\n", + "\n", + "\n", + "\n", + " ## Register Callbacks\n", + " filename = 'output/model_train.csv'\n", + " csv_log = CSVLogger(filename, separator=' ', append=False)\n", + "\n", + " early_stopping = EarlyStopping(\n", + " monitor='loss', patience=early_stopping_patience, verbose=1, mode='min')\n", + "\n", + " #filepath = \"output/best-weights-{epoch:03d}-{loss:.4f}-{acc:.4f}.hdf5\"\n", + " filepath = \"output/best-weights-{epoch:03d}-{val_loss:.4f}.hdf5\"\n", + " checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1,\n", + " save_best_only=True, save_weights_only=False, mode='min', period=1)\n", + "\n", + " tensorboard = TensorBoard(log_dir=logs_path_name, histogram_freq=0, write_graph=True, write_images=True)\n", + "\n", + " callbacks_list = [csv_log, early_stopping, checkpoint, tensorboard]\n", + " logging.info('callbacks_list {}'.format(callbacks_list))\n", + "\n", + "\n", + " ## Generate weights based on images count for each class\n", + " class_weight_val = class_weight.compute_class_weight('balanced', np.unique(train_labels_class), train_labels_class)\n", + " logging.debug('class_weight_val {}'.format(class_weight_val))\n", + "\n", + "\n", + "\n", + "\n", + " input_shape_btl_layer = train_data.shape[1:]\n", + " logging.debug('input_shape_btl_layer {}'.format(input_shape_btl_layer))\n", + " #model = create_model(is_input_bottleneck=True, is_load_weights=False, input_shape, optimizer, learn_rate, decay, momentum, activation, dropout_rate)\n", + " model = create_model(True, False, input_shape_btl_layer, len(class_names), optimizer, learn_rate, decay, momentum, activation, dropout_rate)\n", + "\n", + " logging.info('train_labels_iou {}'.format(train_labels_iou.shape))\n", + " logging.info('train_labels_class {}'.format(train_labels_class.shape))\n", + " logging.info('train_data {}'.format(train_data.shape))\n", + "\n", + "\n", + " logging.info('val_labels_iou {}'.format(val_labels_iou.shape))\n", + " logging.info('val_labels_class {}'.format(val_labels_class.shape))\n", + " logging.info('val_data {}'.format(val_data.shape))\n", + "\n", + " # TODO: class_weight_val wrong\n", + " model.fit(train_data, [train_labels_class, train_labels_iou],\n", + " class_weight=[class_weight_val, class_weight_val], # dictionary mapping classes to a weight value, used for scaling the loss function (during training only).\n", + " epochs=epochs,\n", + " batch_size=batch_size,\n", + " validation_data=(val_data, [val_labels_class, val_labels_iou]),\n", + " callbacks=callbacks_list)\n", + "\n", + " # TODO: These are not the best weights\n", + " model.save_weights(top_model_weights_path_save)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Initialize Input, Output Data\n", + "init()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Save Bottlenecks (As Numpy Arrays)\n", + "save_bottleneck()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "train_model()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:tensorflow_p36]", + "language": "python", + "name": "conda-env-tensorflow_p36-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}