← back to Handbag Authentication
handbag_data/github_datasets/Pursearch/src/.ipynb_checkpoints/object_detection-checkpoint.ipynb
1204 lines
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:11.747429Z",
"start_time": "2020-01-20T02:05:08.094662Z"
}
},
"outputs": [],
"source": [
"import os\n",
"import pathlib\n",
"import numpy as np\n",
"import os\n",
"import six.moves.urllib as urllib\n",
"import sys\n",
"import tarfile\n",
"import tensorflow as tf\n",
"import zipfile\n",
"\n",
"from collections import defaultdict\n",
"from io import StringIO\n",
"from matplotlib import pyplot as plt\n",
"from PIL import Image\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:11.817350Z",
"start_time": "2020-01-20T02:05:11.786782Z"
}
},
"outputs": [],
"source": [
"from object_detection.utils import ops as utils_ops\n",
"from object_detection.utils import label_map_util\n",
"from object_detection.utils import visualization_utils as vis_util"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:11.874676Z",
"start_time": "2020-01-20T02:05:11.869974Z"
}
},
"outputs": [],
"source": [
"# patch tf1 into `utils.ops`\n",
"utils_ops.tf = tf.compat.v1\n",
"\n",
"# Patch the location of gfile\n",
"tf.gfile = tf.io.gfile"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:12.046723Z",
"start_time": "2020-01-20T02:05:12.039758Z"
}
},
"outputs": [],
"source": [
"def load_model(model_name):\n",
" base_url = 'http://download.tensorflow.org/models/object_detection/'\n",
" model_file = model_name + '.tar.gz'\n",
" model_dir = tf.keras.utils.get_file(\n",
" fname=model_name, \n",
" origin=base_url + model_file,\n",
" untar=True)\n",
"\n",
" model_dir = pathlib.Path(model_dir)/\"saved_model\"\n",
"\n",
" model = tf.saved_model.load(str(model_dir))\n",
" model = model.signatures['serving_default']\n",
"\n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:14.458198Z",
"start_time": "2020-01-20T02:05:14.443711Z"
}
},
"outputs": [],
"source": [
"# List of the strings that is used to add correct label for each box.\n",
"PATH_TO_LABELS = '/Users/jianinglu1/Documents/GitHub/models/research/object_detection/data/mscoco_label_map.pbtxt'\n",
"category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:33.718728Z",
"start_time": "2020-01-20T02:05:22.224684Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Saver not created because there are no variables in the graph to restore\n"
]
}
],
"source": [
"model_name = 'faster_rcnn_inception_v2_coco_2018_01_28'\n",
"detection_model = load_model(model_name)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:35.643120Z",
"start_time": "2020-01-20T02:05:35.618782Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'detection_scores': tf.float32,\n",
" 'detection_classes': tf.float32,\n",
" 'num_detections': tf.float32,\n",
" 'detection_boxes': tf.float32}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"detection_model.output_dtypes"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:36.555907Z",
"start_time": "2020-01-20T02:05:36.548916Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'detection_scores': TensorShape([None, 100]),\n",
" 'detection_classes': TensorShape([None, 100]),\n",
" 'num_detections': TensorShape([None]),\n",
" 'detection_boxes': TensorShape([None, 100, 4])}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"detection_model.output_shapes"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:37.518561Z",
"start_time": "2020-01-20T02:05:37.509573Z"
}
},
"outputs": [],
"source": [
"def run_inference_for_single_image(model, image):\n",
" image = np.asarray(image)\n",
" # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.\n",
" input_tensor = tf.convert_to_tensor(image)\n",
" # The model expects a batch of images, so add an axis with `tf.newaxis`.\n",
" input_tensor = input_tensor[tf.newaxis,...]\n",
"\n",
" # Run inference\n",
" output_dict = model(input_tensor)\n",
"\n",
" # All outputs are batches tensors.\n",
" # Convert to numpy arrays, and take index [0] to remove the batch dimension.\n",
" # We're only interested in the first num_detections.\n",
" num_detections = int(output_dict.pop('num_detections'))\n",
" output_dict = {key:value[0, :num_detections].numpy() \n",
" for key,value in output_dict.items()}\n",
" output_dict['num_detections'] = num_detections\n",
"\n",
" # detection_classes should be ints.\n",
" output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)\n",
" \n",
" # Handle models with masks:\n",
" if 'detection_masks' in output_dict:\n",
" # Reframe the the bbox mask to the image size.\n",
" detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(\n",
" output_dict['detection_masks'], output_dict['detection_boxes'],\n",
" image.shape[0], image.shape[1]) \n",
" detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,\n",
" tf.uint8)\n",
" output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()\n",
" \n",
" return output_dict"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:05:38.475609Z",
"start_time": "2020-01-20T02:05:38.468987Z"
}
},
"outputs": [],
"source": [
"def show_inference(model, image_path):\n",
" # the array based representation of the image will be used later in order to prepare the\n",
" # result image with boxes and labels on it.\n",
" image_np = np.array(Image.open(image_path))\n",
" #print(image_np)\n",
" # Actual detection.\n",
" output_dict = run_inference_for_single_image(model, image_np)\n",
" if 31 not in output_dict[\"detection_classes\"]:\n",
" return 0\n",
" else:\n",
" return 1\n",
" #print(output_dict[\"detection_masks_reframed\"])\n",
" #print(output_dict[\"detection_boxes\"])\n",
" # Visualization of the results of a detection.\n",
" vis_util.visualize_boxes_and_labels_on_image_array(\n",
" image_np,\n",
" output_dict['detection_boxes'],\n",
" output_dict['detection_classes'],\n",
" output_dict['detection_scores'],\n",
" category_index,\n",
" instance_masks=output_dict.get('detection_masks_reframed', None),\n",
" use_normalized_coordinates=True,\n",
" line_thickness=8,\n",
" min_score_thresh=0.0\n",
" )\n",
"\n",
" display(Image.fromarray(image_np))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T02:16:02.525295Z",
"start_time": "2020-01-20T02:16:02.515451Z"
}
},
"outputs": [],
"source": [
"def crop_image(name, path, outpath, show=True, save=True):\n",
" image = Image.open(os.path.join(path,name))\n",
" if show:\n",
" image.show()\n",
" width, hight = image.size\n",
" image_np = np.array(image)\n",
" output_dict = run_inference_for_single_image(detection_model, image_np)\n",
" if 31 in output_dict[\"detection_classes\"]:\n",
" max_score = max([output_dict[\"detection_scores\"][idx] for idx, i in enumerate(output_dict[\"detection_classes\"]) if i == 31])\n",
" index = list(output_dict[\"detection_scores\"]).index(max_score) \n",
" [y,x,h,w] = output_dict[\"detection_boxes\"][index]\n",
" cropped = image_np[int(y*hight):int(h*hight), int(x*width):int(w*width)]\n",
" if show:\n",
" img = Image.fromarray(cropped,\"RGB\")\n",
" img.show()\n",
" if save:\n",
" img = Image.fromarray(cropped,\"RGB\")\n",
" img.save(os.path.join(outpath, name))\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T03:56:28.962031Z",
"start_time": "2020-01-20T03:56:28.937093Z"
}
},
"outputs": [],
"source": [
"def rename(path):\n",
" olddir = os.getcwd()\n",
" os.chdir(path)\n",
" images = [ i for i in os.listdir(path) if i.split(\".\")[1] in [\"png\",\"gif\",\"jpeg\",\"jpg\"] ]\n",
" for idx, i in enumerate(images):\n",
" if i.split(\".\")[1] in [\"png\",\"gif\",\"jpeg\"]:\n",
" im = Image.open(i)\n",
" rgb_im = im.convert('RGB')\n",
" rgb_im.save(i.split(\".\")[0] + \".jpg\")\n",
" os.system(\"rm \" + i )\n",
" os.system(\"cp \" + i.split(\".\")[0] + \".jpg \" + str(idx) + \".jpg\")\n",
" os.chdir(olddir)\n",
" return len(images)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T04:21:14.223966Z",
"start_time": "2020-01-20T04:02:58.651519Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"2\n",
"3\n",
"5\n",
"6\n",
"7\n",
"8\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"23\n",
"24\n",
"27\n",
"28\n",
"32\n",
"33\n",
"35\n",
"38\n",
"39\n",
"40\n",
"43\n",
"45\n",
"48\n",
"49\n",
"50\n",
"51\n",
"53\n",
"55\n",
"59\n",
"60\n",
"61\n",
"62\n",
"65\n",
"69\n",
"74\n",
"76\n",
"78\n",
"80\n",
"81\n",
"83\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"91\n",
"94\n",
"95\n",
"100\n",
"101\n",
"102\n",
"105\n",
"106\n",
"107\n",
"109\n",
"110\n",
"112\n",
"113\n",
"114\n",
"115\n",
"116\n",
"117\n",
"120\n",
"124\n",
"127\n",
"128\n",
"129\n",
"130\n",
"134\n",
"138\n",
"139\n",
"140\n",
"142\n",
"144\n",
"147\n",
"148\n",
"151\n",
"152\n",
"154\n",
"158\n",
"159\n",
"164\n",
"165\n",
"169\n",
"171\n",
"172\n",
"175\n",
"176\n",
"177\n",
"179\n",
"183\n",
"184\n",
"185\n",
"187\n",
"189\n",
"192\n",
"193\n",
"194\n",
"198\n",
"199\n",
"202\n",
"203\n",
"204\n",
"205\n",
"206\n",
"207\n",
"212\n",
"213\n",
"215\n",
"216\n",
"217\n",
"218\n",
"219\n",
"220\n",
"221\n",
"223\n",
"224\n",
"225\n",
"226\n",
"227\n",
"229\n",
"230\n",
"231\n",
"232\n",
"233\n",
"234\n",
"2\n",
"4\n",
"5\n",
"7\n",
"9\n",
"11\n",
"16\n",
"17\n",
"19\n",
"20\n",
"21\n",
"23\n",
"26\n",
"30\n",
"31\n",
"33\n",
"34\n",
"37\n",
"38\n",
"40\n",
"41\n",
"43\n",
"44\n",
"46\n",
"48\n",
"50\n",
"51\n",
"54\n",
"55\n",
"56\n",
"57\n",
"59\n",
"61\n",
"62\n",
"67\n",
"68\n",
"70\n",
"71\n",
"74\n",
"81\n",
"83\n",
"84\n",
"88\n",
"89\n",
"90\n",
"95\n",
"99\n",
"100\n",
"102\n",
"108\n",
"109\n",
"110\n",
"111\n",
"113\n",
"116\n",
"118\n",
"120\n",
"121\n",
"122\n",
"127\n",
"129\n",
"132\n",
"133\n",
"136\n",
"138\n",
"139\n",
"140\n",
"142\n",
"149\n",
"151\n",
"152\n",
"153\n",
"154\n",
"155\n",
"157\n",
"158\n",
"159\n",
"160\n",
"161\n",
"163\n",
"169\n",
"173\n",
"175\n",
"176\n",
"178\n",
"179\n",
"180\n",
"182\n",
"186\n",
"189\n",
"190\n",
"191\n",
"193\n",
"194\n",
"195\n",
"196\n",
"198\n",
"199\n",
"202\n",
"203\n",
"205\n",
"212\n",
"213\n",
"214\n",
"215\n",
"218\n",
"219\n",
"221\n",
"222\n",
"223\n",
"224\n",
"225\n",
"226\n",
"227\n",
"229\n",
"233\n",
"236\n",
"238\n",
"240\n",
"241\n",
"242\n",
"246\n",
"251\n",
"252\n",
"253\n",
"254\n",
"257\n",
"261\n",
"264\n",
"265\n",
"268\n",
"269\n",
"270\n",
"272\n",
"273\n",
"276\n",
"277\n",
"285\n",
"287\n",
"290\n",
"293\n",
"295\n",
"296\n",
"297\n",
"298\n",
"299\n",
"303\n",
"307\n",
"312\n",
"316\n",
"318\n",
"319\n",
"320\n",
"324\n",
"328\n",
"329\n",
"335\n",
"336\n",
"337\n",
"345\n",
"346\n",
"348\n",
"350\n",
"352\n",
"353\n",
"354\n",
"361\n",
"365\n",
"367\n",
"368\n",
"369\n",
"370\n",
"1\n",
"2\n",
"3\n",
"6\n",
"8\n",
"9\n",
"10\n",
"11\n",
"13\n",
"15\n",
"16\n",
"19\n",
"21\n",
"22\n",
"23\n",
"25\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"37\n",
"38\n",
"39\n",
"40\n",
"42\n",
"43\n",
"45\n",
"46\n",
"47\n",
"51\n",
"52\n",
"53\n",
"54\n",
"55\n",
"57\n",
"58\n",
"61\n",
"62\n",
"64\n",
"65\n",
"66\n",
"67\n",
"69\n",
"73\n",
"75\n",
"76\n",
"77\n",
"80\n",
"82\n",
"83\n",
"86\n",
"90\n",
"92\n",
"97\n",
"100\n",
"101\n",
"102\n",
"103\n",
"105\n",
"106\n",
"109\n",
"110\n",
"111\n",
"112\n",
"113\n",
"115\n",
"117\n",
"119\n",
"126\n",
"128\n",
"130\n",
"132\n",
"133\n",
"136\n",
"137\n",
"140\n",
"142\n",
"143\n",
"144\n",
"145\n",
"146\n",
"149\n",
"150\n",
"151\n",
"153\n",
"155\n",
"156\n",
"157\n",
"159\n",
"161\n",
"162\n",
"165\n",
"166\n",
"169\n",
"170\n",
"171\n",
"172\n",
"174\n",
"177\n",
"178\n",
"179\n",
"180\n",
"182\n",
"183\n",
"184\n",
"187\n",
"188\n",
"189\n",
"190\n",
"195\n",
"199\n",
"202\n",
"203\n",
"205\n",
"211\n",
"212\n",
"215\n",
"216\n",
"218\n",
"219\n",
"224\n",
"225\n",
"226\n",
"227\n",
"228\n",
"230\n",
"232\n",
"238\n",
"239\n",
"241\n",
"242\n",
"244\n",
"245\n",
"246\n",
"252\n",
"255\n",
"258\n",
"260\n",
"261\n",
"262\n",
"263\n",
"264\n",
"266\n",
"271\n",
"273\n",
"275\n",
"276\n",
"277\n",
"279\n",
"280\n",
"281\n",
"282\n",
"283\n",
"287\n",
"290\n",
"291\n",
"294\n",
"296\n",
"297\n",
"298\n",
"301\n",
"306\n",
"307\n",
"308\n",
"310\n",
"311\n",
"313\n",
"314\n",
"315\n",
"316\n",
"317\n",
"318\n",
"320\n",
"321\n",
"323\n",
"327\n",
"330\n",
"331\n",
"333\n",
"336\n",
"340\n",
"341\n",
"343\n",
"344\n",
"345\n",
"349\n",
"351\n",
"353\n",
"355\n",
"356\n",
"359\n",
"362\n",
"363\n",
"366\n",
"369\n",
"370\n",
"371\n",
"375\n"
]
}
],
"source": [
"for i in [\"jetsettote\",\"mercertotebag\",\"selmasatchel\"]:\n",
" number = rename(\"/Users/jianinglu1/Documents/GitHub/DataInsight/data/clean/MK/\" + i)\n",
" path = \"/Users/jianinglu1/Documents/GitHub/DataInsight/data/clean/MK/\" + i\n",
" outpath = \"/Users/jianinglu1/Documents/GitHub/DataInsight/data/processed/MK/\" + i\n",
" for i in range(number):\n",
" name = str(i) + \".jpg\"\n",
" try:\n",
" if not crop_image(name, path, outpath, show=False):\n",
" print(i)\n",
" except:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-20T04:01:13.786270Z",
"start_time": "2020-01-20T03:56:32.630751Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"14\n",
"15\n",
"16\n",
"18\n",
"19\n",
"20\n",
"21\n",
"25\n",
"26\n",
"27\n",
"29\n",
"30\n",
"31\n",
"32\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"54\n",
"55\n",
"56\n",
"57\n",
"58\n",
"61\n",
"62\n",
"64\n",
"65\n",
"67\n",
"68\n",
"69\n",
"74\n",
"75\n",
"76\n",
"78\n",
"79\n",
"82\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"93\n",
"94\n",
"95\n",
"96\n",
"98\n",
"99\n",
"102\n",
"103\n",
"104\n",
"105\n",
"106\n",
"107\n",
"108\n",
"110\n",
"111\n",
"112\n",
"115\n",
"116\n",
"117\n",
"118\n",
"119\n",
"120\n",
"121\n",
"122\n",
"123\n",
"126\n",
"127\n",
"130\n",
"132\n",
"134\n",
"135\n",
"137\n",
"138\n",
"139\n",
"140\n",
"142\n",
"143\n",
"144\n",
"145\n",
"147\n",
"148\n",
"151\n",
"152\n",
"153\n",
"154\n",
"155\n",
"156\n",
"157\n",
"158\n",
"160\n",
"161\n",
"165\n",
"166\n",
"167\n",
"169\n",
"171\n",
"172\n",
"173\n",
"174\n",
"175\n",
"177\n",
"179\n",
"180\n",
"181\n",
"184\n",
"185\n",
"186\n",
"187\n",
"188\n",
"189\n",
"190\n",
"193\n",
"194\n",
"195\n",
"196\n",
"197\n",
"198\n",
"201\n",
"202\n",
"203\n",
"204\n",
"205\n",
"206\n",
"207\n",
"208\n",
"209\n",
"210\n",
"211\n",
"212\n",
"214\n",
"215\n",
"216\n",
"219\n",
"223\n",
"225\n",
"226\n",
"227\n",
"228\n",
"229\n",
"232\n",
"235\n",
"236\n",
"237\n",
"239\n",
"240\n",
"241\n",
"242\n",
"243\n",
"245\n",
"246\n",
"247\n",
"248\n",
"249\n",
"250\n",
"251\n",
"252\n",
"253\n",
"254\n",
"256\n",
"258\n",
"259\n",
"260\n",
"261\n",
"262\n",
"263\n",
"264\n",
"265\n",
"266\n",
"269\n",
"270\n",
"271\n",
"272\n",
"274\n",
"275\n",
"276\n",
"277\n",
"279\n",
"280\n",
"281\n",
"282\n",
"283\n",
"284\n",
"288\n",
"291\n",
"292\n",
"293\n",
"294\n",
"295\n",
"296\n",
"297\n",
"298\n",
"300\n",
"302\n",
"304\n",
"306\n",
"307\n",
"308\n",
"310\n",
"312\n",
"314\n",
"316\n",
"318\n",
"319\n",
"321\n",
"322\n",
"323\n",
"324\n",
"327\n",
"328\n",
"329\n",
"330\n",
"331\n",
"332\n"
]
}
],
"source": [
"path = \"/Users/jianinglu1/Documents/GitHub/DataInsight/data/clean/Gucci/sylvie\"\n",
"outpath = \"/Users/jianinglu1/Documents/GitHub/DataInsight/data/processed/Gucci/sylvie\"\n",
"for i in range(333):\n",
" name = str(i) + \".jpg\"\n",
" try:\n",
" if not crop_image(name, path, outpath, show=False):\n",
" print(i)\n",
" except:\n",
" print(i)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.4"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}