Spaces:
Sleeping
Sleeping
File size: 29,858 Bytes
3a9ad40 a657511 3a9ad40 a657511 3a9ad40 a657511 3a9ad40 a657511 3a9ad40 a657511 3a9ad40 a657511 3a9ad40 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"# File renaming for consistency\n",
"import os\n",
"from os import path\n",
"\n",
"for count, filename in enumerate(os.listdir(\"deadlift2\")):\n",
" src = \"deadlift2/\" + filename\n",
" string = \"deadlift2/deadlift_\" + str(count) + \".jpg\"\n",
" os.rename(src, string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-to-classify-photos-of-dogs-and-cats/"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(549,) ()\n"
]
}
],
"source": [
"from os import listdir\n",
"from numpy import asarray\n",
"from numpy import save\n",
"from keras.preprocessing.image import load_img\n",
"from keras.preprocessing.image import img_to_array\n",
"\n",
"folder = \"train/\"\n",
"photos, labels = list(), list()\n",
"\n",
"for file in listdir(folder):\n",
" output = 0.0\n",
" if file.startswith(\"squat\"):\n",
" output = 1.0\n",
" if file.startswith(\"deadlift\"):\n",
" output = 2.0\n",
" photo = load_img(folder + file, target_size=(150,150))\n",
" photo = img_to_array\n",
" \n",
" photos.append(photo)\n",
" labels.append(output)\n",
"photos = asarray(photos)\n",
"labels = asarray(output)\n",
"print(photos.shape, labels.shape)\n",
"\n",
"save(\"exercise_photos.npy\", photos)\n",
"save(\"exercise_labels.npy\", photos)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(549,) (549,)\n"
]
}
],
"source": [
"from numpy import load\n",
"photos = load(\"exercise_photos.npy\",allow_pickle=True)\n",
"labels = load(\"exercise_labels.npy\",allow_pickle=True)\n",
"\n",
"print(photos.shape, labels.shape)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"# Directory Generation\n",
"from os import makedirs\n",
"dataset_home = \"dataset/\"\n",
"subdirs = [\"train/\", \"test/\"]\n",
"for subdir in subdirs:\n",
" # create label subdirectories\n",
" labeldirs = [\"bench/\", \"squat/\", \"deadlift/\"]\n",
" for labldir in labeldirs:\n",
" newdir = dataset_home + subdir + labldir\n",
" makedirs(newdir)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"# Segment into testing and training images\n",
"import random\n",
"from shutil import copyfile\n",
"random.seed(1)\n",
"ratio = 0.2\n",
"dataset_home = \"dataset/\"\n",
"src_directory = \"images/\"\n",
"for file in listdir(src_directory):\n",
" src = src_directory + '/' + file\n",
" dst_dir = \"train/\"\n",
" if random.random() < ratio:\n",
" dst_dir = \"test/\"\n",
" if file.startswith(\"bench\"):\n",
" dst = dataset_home + dst_dir + \"bench/\" + file\n",
" elif file.startswith(\"squat\"):\n",
" dst = dataset_home + dst_dir + \"squat/\" + file\n",
" else:\n",
" dst = dataset_home + dst_dir + \"deadlift/\" + file\n",
" copyfile(src, dst) "
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 448 images belonging to 3 classes.\n",
"Found 101 images belonging to 3 classes.\n"
]
},
{
"ename": "ValueError",
"evalue": "in user code:\n\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:806 train_function *\n return step_function(self, iterator)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:796 step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:1211 run\n return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2585 call_for_each_replica\n return self._call_for_each_replica(fn, args, kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2945 _call_for_each_replica\n return fn(*args, **kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:789 run_step **\n outputs = model.train_step(data)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:748 train_step\n loss = self.compiled_loss(\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\compile_utils.py:204 __call__\n loss_value = loss_obj(y_t, y_p, sample_weight=sw)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:149 __call__\n losses = ag_call(y_true, y_pred)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:253 call **\n return ag_fn(y_true, y_pred, **self._fn_kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n return target(*args, **kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:1535 categorical_crossentropy\n return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n return target(*args, **kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\backend.py:4687 categorical_crossentropy\n target.shape.assert_is_compatible_with(output.shape)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\framework\\tensor_shape.py:1134 assert_is_compatible_with\n raise ValueError(\"Shapes %s and %s are incompatible\" % (self, other))\n\n ValueError: Shapes (None, 1) and (None, 10) are incompatible\n",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-58-08697642c99a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 99\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 100\u001b[0m \u001b[1;31m# entry point, run the test harness\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 101\u001b[1;33m \u001b[0mrun_test_harness\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-58-08697642c99a>\u001b[0m in \u001b[0;36mrun_test_harness\u001b[1;34m()\u001b[0m\n\u001b[0;32m 90\u001b[0m class_mode='binary', batch_size=64, target_size=(150, 150))\n\u001b[0;32m 91\u001b[0m \u001b[1;31m# fit model\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 92\u001b[1;33m history = model.fit(train_it, steps_per_epoch=len(train_it),\n\u001b[0m\u001b[0;32m 93\u001b[0m validation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n\u001b[0;32m 94\u001b[0m \u001b[1;31m# evaluate model\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py\u001b[0m in \u001b[0;36m_method_wrapper\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 106\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_method_wrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 107\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_in_multi_worker_mode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# pylint: disable=protected-access\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 108\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 109\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 110\u001b[0m \u001b[1;31m# Running inside `run_distribute_coordinator` already.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[0;32m 1096\u001b[0m batch_size=batch_size):\n\u001b[0;32m 1097\u001b[0m \u001b[0mcallbacks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mon_train_batch_begin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1098\u001b[1;33m \u001b[0mtmp_logs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtrain_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1099\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mdata_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshould_sync\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1100\u001b[0m \u001b[0mcontext\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0masync_wait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m 778\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 779\u001b[0m \u001b[0mcompiler\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"nonXla\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 780\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 781\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 782\u001b[0m \u001b[0mnew_tracing_count\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_tracing_count\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m_call\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m 821\u001b[0m \u001b[1;31m# This is the first call of __call__, so we have to initialize.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 822\u001b[0m \u001b[0minitializers\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 823\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_initialize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0madd_initializers_to\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minitializers\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 824\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 825\u001b[0m \u001b[1;31m# At this point we know that the initialization is complete (or less\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m_initialize\u001b[1;34m(self, args, kwds, add_initializers_to)\u001b[0m\n\u001b[0;32m 694\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_graph_deleter\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mFunctionDeleter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_lifted_initializer_graph\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 695\u001b[0m self._concrete_stateful_fn = (\n\u001b[1;32m--> 696\u001b[1;33m self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access\n\u001b[0m\u001b[0;32m 697\u001b[0m *args, **kwds))\n\u001b[0;32m 698\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_get_concrete_function_internal_garbage_collected\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 2853\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2854\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_lock\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2855\u001b[1;33m \u001b[0mgraph_function\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_maybe_define_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2856\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2857\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_maybe_define_function\u001b[1;34m(self, args, kwargs)\u001b[0m\n\u001b[0;32m 3211\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3212\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_function_cache\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmissed\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcall_context_key\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3213\u001b[1;33m \u001b[0mgraph_function\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_create_graph_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3214\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_function_cache\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprimary\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcache_key\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3215\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_create_graph_function\u001b[1;34m(self, args, kwargs, override_flat_arg_shapes)\u001b[0m\n\u001b[0;32m 3063\u001b[0m \u001b[0marg_names\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbase_arg_names\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mmissing_arg_names\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3064\u001b[0m graph_function = ConcreteFunction(\n\u001b[1;32m-> 3065\u001b[1;33m func_graph_module.func_graph_from_py_func(\n\u001b[0m\u001b[0;32m 3066\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_name\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3067\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_python_function\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\framework\\func_graph.py\u001b[0m in \u001b[0;36mfunc_graph_from_py_func\u001b[1;34m(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)\u001b[0m\n\u001b[0;32m 984\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moriginal_func\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_decorator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munwrap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpython_func\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 985\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 986\u001b[1;33m \u001b[0mfunc_outputs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpython_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mfunc_args\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mfunc_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 987\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 988\u001b[0m \u001b[1;31m# invariant: `func_outputs` contains only Tensors, CompositeTensors,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36mwrapped_fn\u001b[1;34m(*args, **kwds)\u001b[0m\n\u001b[0;32m 598\u001b[0m \u001b[1;31m# __wrapped__ allows AutoGraph to swap in a converted function. We give\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 599\u001b[0m \u001b[1;31m# the function a weak reference to itself to avoid a reference cycle.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 600\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mweak_wrapped_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__wrapped__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 601\u001b[0m \u001b[0mweak_wrapped_fn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mweakref\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mref\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwrapped_fn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 602\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\python38-64\\lib\\site-packages\\tensorflow\\python\\framework\\func_graph.py\u001b[0m in \u001b[0;36mwrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 971\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# pylint:disable=broad-except\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 972\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"ag_error_metadata\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 973\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mag_error_metadata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mto_exception\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 974\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 975\u001b[0m \u001b[1;32mraise\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: in user code:\n\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:806 train_function *\n return step_function(self, iterator)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:796 step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:1211 run\n return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2585 call_for_each_replica\n return self._call_for_each_replica(fn, args, kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2945 _call_for_each_replica\n return fn(*args, **kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:789 run_step **\n outputs = model.train_step(data)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:748 train_step\n loss = self.compiled_loss(\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\compile_utils.py:204 __call__\n loss_value = loss_obj(y_t, y_p, sample_weight=sw)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:149 __call__\n losses = ag_call(y_true, y_pred)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:253 call **\n return ag_fn(y_true, y_pred, **self._fn_kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n return target(*args, **kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:1535 categorical_crossentropy\n return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n return target(*args, **kwargs)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\keras\\backend.py:4687 categorical_crossentropy\n target.shape.assert_is_compatible_with(output.shape)\n c:\\python38-64\\lib\\site-packages\\tensorflow\\python\\framework\\tensor_shape.py:1134 assert_is_compatible_with\n raise ValueError(\"Shapes %s and %s are incompatible\" % (self, other))\n\n ValueError: Shapes (None, 1) and (None, 10) are incompatible\n"
]
}
],
"source": [
"# Baseline CNN Model\n",
"import sys\n",
"from matplotlib import pyplot\n",
"import keras\n",
"from keras.utils import to_categorical\n",
"from keras.models import Sequential\n",
"from keras.layers import Conv2D\n",
"from keras.layers import MaxPooling2D\n",
"from keras.layers import Dense\n",
"from keras.layers import Flatten\n",
"from keras.layers import Dropout\n",
"from keras.optimizers import SGD\n",
"from keras.preprocessing.image import ImageDataGenerator\n",
" \n",
"# one block VGG\n",
"\"\"\"\n",
"def define_model():\n",
" model = Sequential()\n",
" model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(150, 150, 3)))\n",
" model.add(MaxPooling2D((2, 2)))\n",
" model.add(Flatten())\n",
" model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n",
" model.add(Dense(1, activation='sigmoid'))\n",
" # compile model\n",
" opt = SGD(lr=0.001, momentum=0.9)\n",
" model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n",
" return model\n",
"\"\"\"\n",
"\"\"\"\n",
"# two block VGG\n",
"def define_model():\n",
" model = Sequential()\n",
" model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(150, 150, 3)))\n",
" model.add(MaxPooling2D((2, 2)))\n",
" model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n",
" model.add(MaxPooling2D((2, 2)))\n",
" model.add(Flatten())\n",
" model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n",
" model.add(Dense(1, activation='sigmoid'))\n",
" # compile model\n",
" opt = SGD(lr=0.001, momentum=0.9)\n",
" model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n",
" return model\n",
"\"\"\"\n",
"# three block VGG\n",
"def define_model():\n",
"\n",
" cnn1 = Sequential()\n",
" cnn1.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))\n",
" cnn1.add(MaxPooling2D((2, 2)))\n",
" cnn1.add(Dropout(0.2))\n",
"\n",
" cnn1.add(Flatten())\n",
"\n",
" cnn1.add(Dense(128, activation='relu'))\n",
" cnn1.add(Dense(10, activation='softmax'))\n",
"\n",
" cnn1.compile(loss=keras.losses.categorical_crossentropy,\n",
" optimizer=keras.optimizers.Adam(),\n",
" metrics=['accuracy'])\n",
" return cnn1\n",
"\n",
"# plot diagnostic learning curves\n",
"def summarize_diagnostics(history):\n",
" # plot loss\n",
" pyplot.subplot(211)\n",
" pyplot.title('Cross Entropy Loss')\n",
" pyplot.plot(history.history['loss'], color='blue', label='train')\n",
" pyplot.plot(history.history['val_loss'], color='orange', label='test')\n",
" # plot accuracy\n",
" pyplot.subplot(212)\n",
" pyplot.title('Classification Accuracy')\n",
" pyplot.plot(history.history['accuracy'], color='blue', label='train')\n",
" pyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n",
" # save plot to file\n",
" filename = sys.argv[0].split('/')[-1]\n",
" pyplot.savefig(filename + '_plot.png')\n",
" pyplot.close()\n",
" \n",
"# run the test harness for evaluating a model\n",
"def run_test_harness():\n",
" # define model\n",
" model = define_model()\n",
" # create data generator\n",
" datagen = ImageDataGenerator(rescale=1.0/255.0)\n",
" # prepare iterators\n",
" train_it = datagen.flow_from_directory('dataset/train/',\n",
" class_mode='binary', batch_size=64, target_size=(150, 150))\n",
" test_it = datagen.flow_from_directory('dataset/test/',\n",
" class_mode='binary', batch_size=64, target_size=(150, 150))\n",
" # fit model\n",
" history = model.fit(train_it, steps_per_epoch=len(train_it),\n",
" validation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n",
" # evaluate model\n",
" _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)\n",
" print('> %.3f' % (acc * 100.0))\n",
" # learning curves\n",
" summarize_diagnostics(history)\n",
" \n",
"# entry point, run the test harness\n",
"run_test_harness()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|