├── README.md ├── requirements.txt ├── train_webcam.py └── training.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # ml4pi 2 | 3 | A tiny framework for doing machine learning on Raspberry Pi. 4 | 5 | 6 | ## Setup the Pi 7 | 8 | Download the latest version of Raspbian and flash your micro SD card with [Etcher](https://etcher.io/) 9 | 10 | Add blank file called `ssh` into the root of the SD disk and a file called `wpa_supplicant.conf` containing the following (replace with your wifi details): 11 | 12 | 13 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 14 | update_config=1 15 | 16 | network={ 17 | ssid="YOUR_WIFI_NETWORK" 18 | psk="YOUR_WIFI_PASSWORD" 19 | } 20 | 21 | 22 | In terminal ssh into the pi: 23 | 24 | sudo ssh pi@raspberrypi.local 25 | 26 | Default password is 'raspberry'. To change password use the `passwd` command. 27 | 28 | Update the pi: 29 | 30 | sudo apt-get update && sudo apt-get upgrade 31 | 32 | Install nodejs: 33 | 34 | sudo apt-get install nodejs npm git-core 35 | 36 | Optional: install nettalk for easy file sharing: 37 | 38 | sudo apt-get install netatalk 39 | 40 | Reboot: 41 | 42 | sudo reboot 43 | 44 | 45 | ## Install software 46 | 47 | Install pre-requisites: 48 | 49 | sudo apt install libatlas-base-dev libjasper-dev libqtgui4 libqt4-test libhdf5-dev 50 | 51 | Make sure you enable your camera through `sudo raspi-config`. Reboot again afterwards. 52 | 53 | Clone this library: 54 | 55 | git clone https://github.com/ml4a/ml4pi 56 | 57 | Install all the required python libraries: 58 | 59 | cd ml4pi 60 | pip3 install -r requirements.txt 61 | 62 | Try running the interactive trainer: 63 | 64 | python3 train_webcam.py 65 | 66 | 67 | ## Todo 68 | 69 | - [get picamera at faster fps](https://www.pyimagesearch.com/2015/12/28/increasing-raspberry-pi-fps-with-python-and-opencv/) 70 | - train from a directory of images 71 | - saving/loading models 72 | - deployment script (load model, then continuously predict samples) 73 | 74 | 75 | ## Training a dataset from a folder of images 76 | 77 | (not finished yet) 78 | 79 | Example is using a dataset which can be obtained: 80 | 81 | wget http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz 82 | tar -xzf 101_ObjectCategories.tar.gz -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tensorflow 2 | keras 3 | opencv-python 4 | Pillow 5 | sklearn 6 | h5py 7 | picamera[array] -------------------------------------------------------------------------------- /train_webcam.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import cv2 4 | import picamera 5 | import picamera.array 6 | from PIL import Image 7 | import keras 8 | from keras import backend as K 9 | from keras.layers.core import Dense 10 | from keras.optimizers import Adam 11 | from keras.metrics import categorical_crossentropy 12 | from keras.preprocessing.image import ImageDataGenerator 13 | from keras.preprocessing import image 14 | from keras.models import Model 15 | from keras.applications import imagenet_utils 16 | from sklearn.metrics import confusion_matrix 17 | from keras.models import Sequential 18 | from keras.layers import Dense, Activation, Flatten, Input 19 | 20 | NUM_CLASSES = 2 21 | LEARNING_RATE = 0.0001 22 | EPOCHS = 20 23 | DENSE_UNITS = 100 24 | 25 | TRAINING_DATA = [] # Example array to be trained 26 | TRAINING_LABELS = [] # Label array 27 | 28 | 29 | # Load mobilenet model 30 | def loadModel(): 31 | mobilenet = keras.applications.mobilenet.MobileNet() 32 | flatten = Flatten(input_shape=(7,7,1024))(mobilenet.get_layer('conv_pw_13_relu').output) 33 | fc1 = Dense(DENSE_UNITS, activation='relu')(flatten) 34 | fc2 = Dense(NUM_CLASSES)(flatten) 35 | output = Activation('softmax')(fc2) 36 | model = Model(mobilenet.input, output) 37 | # make all layers untrainable by freezing weights (except for last two layers) 38 | for l, layer in enumerate(model.layers[:-3]): 39 | layer.trainable = False 40 | # ensure the last layer is trainable/not frozen 41 | for l, layer in enumerate(model.layers[-3:]): 42 | layer.trainable = True 43 | return model 44 | 45 | # function prepares an image to keras 46 | def prepare_frame(frame): 47 | img = Image.fromarray(frame, 'RGB') 48 | img = img.resize((224,224)) 49 | img_array = np.array(img) 50 | img_array_extended = np.expand_dims(img_array, axis=0).astype('float32') 51 | processed = keras.applications.mobilenet.preprocess_input(img_array_extended) 52 | return processed 53 | 54 | 55 | def addExample(example, label): # add examples to training data set 56 | encoded_y = keras.utils.np_utils.to_categorical(label,num_classes=NUM_CLASSES) # make one-hot 57 | TRAINING_LABELS.append(encoded_y) 58 | TRAINING_DATA.append(example[0]) 59 | print('add example for label %d'%label) 60 | 61 | 62 | # load the model 63 | model = loadModel() 64 | model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 65 | 66 | # start camera 67 | with picamera.PiCamera() as camera: 68 | with picamera.array.PiRGBArray(camera) as output: 69 | while True: 70 | camera.capture(output, 'rgb') 71 | print('Captured %dx%d image' % (output.array.shape[1], output.array.shape[0])) 72 | 73 | frame = output.array 74 | 75 | cv2.imshow('capturing',frame) 76 | key=cv2.waitKey(1) 77 | 78 | if key == ord('1'): # add example in class 0 79 | addExample(prepare_frame(frame), 0) 80 | 81 | if key == ord('2'): # add example in class 1 82 | addExample(prepare_frame(frame), 1) 83 | 84 | if key == ord('t'): # train 85 | model.fit(np.array(TRAINING_DATA), np.array(TRAINING_LABELS), epochs=EPOCHS, batch_size=8) 86 | 87 | if key == ord('p'): # predict 88 | processed_image = prepare_frame(frame) # prepare frame 89 | prediction = model.predict(processed_image) 90 | result = np.argmax(prediction) #imagenet_utils.decode_predictions(prediction) 91 | print('predict %d'%result) 92 | 93 | if key == ord('q'): # quit 94 | break 95 | 96 | output.truncate(0) 97 | 98 | -------------------------------------------------------------------------------- /training.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All of these libraries should import." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import os\n", 17 | "from os import listdir\n", 18 | "from os.path import isfile, join\n", 19 | "\n", 20 | "os.environ['KERAS_BACKEND'] = 'tensorflow'\n", 21 | "#os.environ['KERAS_BACKEND'] = 'theano'\n", 22 | "\n", 23 | "import random\n", 24 | "import numpy as np\n", 25 | "import keras\n", 26 | "import matplotlib.pyplot as plt\n", 27 | "from keras.preprocessing import image\n", 28 | "from keras.applications.imagenet_utils import preprocess_input\n", 29 | "from keras.applications import mobilenet\n", 30 | "from keras.models import Sequential\n", 31 | "from keras.layers import Dense, Dropout, Flatten, Activation\n", 32 | "from keras.layers import Conv2D, MaxPooling2D\n", 33 | "from keras.models import Model" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "This helper function will load and pre-process the image, and return it and the pre-processed pixel vector." 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "def get_image(path, w, h):\n", 50 | " img = image.load_img(path, target_size=(w, h))\n", 51 | " x = image.img_to_array(img)\n", 52 | " x = np.expand_dims(x, axis=0)\n", 53 | " x = preprocess_input(x)\n", 54 | " return img, x\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "We'll set our image width and height to be 224 x 224." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "w, h = 224, 224" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "Let's load all the data. We'll use a three-class classifier as an example from 101ObjectCategories: " 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 5, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "categories = [\"airplanes\", \"crocodile\", \"dragonfly\"]\n", 87 | "data = []\n", 88 | "for c, category in enumerate(categories):\n", 89 | " path = '101_ObjectCategories/%s' % category\n", 90 | " images = [f for f in listdir(path) if isfile(join(path, f))]\n", 91 | " for img_path in images:\n", 92 | " img, x = get_image(join(path, img_path), w, h)\n", 93 | " data.append({'x':np.array(x[0]), 'y':c})\n", 94 | " \n", 95 | "num_classes = len(categories)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "We split the data into training set, validation set, and test set (70%, 15%, 15%). We also pre-process the data by normalizing it and casting to float32. We " 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 6, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "finished loading 918 images from 3 categories\n", 115 | "train / validation / test split: 642, 138, 138\n", 116 | "training data shape: (642, 224, 224, 3)\n", 117 | "training labels shape: (642, 3)\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "train_split, val_split = 0.7, 0.15\n", 123 | "\n", 124 | "\n", 125 | "# create training / validation / test split (70%, 15%, 15%)\n", 126 | "idx_val = int(train_split * len(data))\n", 127 | "idx_test = int((train_split + val_split) * len(data))\n", 128 | "train = data[:idx_val]\n", 129 | "val = data[idx_val:idx_test]\n", 130 | "test = data[idx_test:]\n", 131 | "\n", 132 | "# separate data for labels\n", 133 | "x_train, y_train = np.array([t[\"x\"] for t in train]), [t[\"y\"] for t in train]\n", 134 | "x_val, y_val = np.array([t[\"x\"] for t in val]), [t[\"y\"] for t in val]\n", 135 | "x_test, y_test = np.array([t[\"x\"] for t in test]), [t[\"y\"] for t in test]\n", 136 | "\n", 137 | "# normalize data\n", 138 | "x_train = x_train.astype('float32') / 255.\n", 139 | "x_val = x_val.astype('float32') / 255.\n", 140 | "x_test = x_test.astype('float32') / 255.\n", 141 | "\n", 142 | "# convert labels to one-hot vectors\n", 143 | "y_train = keras.utils.to_categorical(y_train, num_classes)\n", 144 | "y_val = keras.utils.to_categorical(y_val, num_classes)\n", 145 | "y_test = keras.utils.to_categorical(y_test, num_classes)\n", 146 | "\n", 147 | "# summary\n", 148 | "print(\"finished loading %d images from %d categories\"%(len(data), num_classes))\n", 149 | "print(\"train / validation / test split: %d, %d, %d\"%(len(x_train), len(x_val), len(x_test)))\n", 150 | "print(\"training data shape: \", x_train.shape)\n", 151 | "print(\"training labels shape: \", y_train.shape)\n" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "Load mobilenet model." 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 7, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "_________________________________________________________________\n", 171 | "Layer (type) Output Shape Param # \n", 172 | "=================================================================\n", 173 | "input_1 (InputLayer) (None, 224, 224, 3) 0 \n", 174 | "_________________________________________________________________\n", 175 | "conv1_pad (ZeroPadding2D) (None, 226, 226, 3) 0 \n", 176 | "_________________________________________________________________\n", 177 | "conv1 (Conv2D) (None, 112, 112, 32) 864 \n", 178 | "_________________________________________________________________\n", 179 | "conv1_bn (BatchNormalization (None, 112, 112, 32) 128 \n", 180 | "_________________________________________________________________\n", 181 | "conv1_relu (Activation) (None, 112, 112, 32) 0 \n", 182 | "_________________________________________________________________\n", 183 | "conv_pad_1 (ZeroPadding2D) (None, 114, 114, 32) 0 \n", 184 | "_________________________________________________________________\n", 185 | "conv_dw_1 (DepthwiseConv2D) (None, 112, 112, 32) 288 \n", 186 | "_________________________________________________________________\n", 187 | "conv_dw_1_bn (BatchNormaliza (None, 112, 112, 32) 128 \n", 188 | "_________________________________________________________________\n", 189 | "conv_dw_1_relu (Activation) (None, 112, 112, 32) 0 \n", 190 | "_________________________________________________________________\n", 191 | "conv_pw_1 (Conv2D) (None, 112, 112, 64) 2048 \n", 192 | "_________________________________________________________________\n", 193 | "conv_pw_1_bn (BatchNormaliza (None, 112, 112, 64) 256 \n", 194 | "_________________________________________________________________\n", 195 | "conv_pw_1_relu (Activation) (None, 112, 112, 64) 0 \n", 196 | "_________________________________________________________________\n", 197 | "conv_pad_2 (ZeroPadding2D) (None, 114, 114, 64) 0 \n", 198 | "_________________________________________________________________\n", 199 | "conv_dw_2 (DepthwiseConv2D) (None, 56, 56, 64) 576 \n", 200 | "_________________________________________________________________\n", 201 | "conv_dw_2_bn (BatchNormaliza (None, 56, 56, 64) 256 \n", 202 | "_________________________________________________________________\n", 203 | "conv_dw_2_relu (Activation) (None, 56, 56, 64) 0 \n", 204 | "_________________________________________________________________\n", 205 | "conv_pw_2 (Conv2D) (None, 56, 56, 128) 8192 \n", 206 | "_________________________________________________________________\n", 207 | "conv_pw_2_bn (BatchNormaliza (None, 56, 56, 128) 512 \n", 208 | "_________________________________________________________________\n", 209 | "conv_pw_2_relu (Activation) (None, 56, 56, 128) 0 \n", 210 | "_________________________________________________________________\n", 211 | "conv_pad_3 (ZeroPadding2D) (None, 58, 58, 128) 0 \n", 212 | "_________________________________________________________________\n", 213 | "conv_dw_3 (DepthwiseConv2D) (None, 56, 56, 128) 1152 \n", 214 | "_________________________________________________________________\n", 215 | "conv_dw_3_bn (BatchNormaliza (None, 56, 56, 128) 512 \n", 216 | "_________________________________________________________________\n", 217 | "conv_dw_3_relu (Activation) (None, 56, 56, 128) 0 \n", 218 | "_________________________________________________________________\n", 219 | "conv_pw_3 (Conv2D) (None, 56, 56, 128) 16384 \n", 220 | "_________________________________________________________________\n", 221 | "conv_pw_3_bn (BatchNormaliza (None, 56, 56, 128) 512 \n", 222 | "_________________________________________________________________\n", 223 | "conv_pw_3_relu (Activation) (None, 56, 56, 128) 0 \n", 224 | "_________________________________________________________________\n", 225 | "conv_pad_4 (ZeroPadding2D) (None, 58, 58, 128) 0 \n", 226 | "_________________________________________________________________\n", 227 | "conv_dw_4 (DepthwiseConv2D) (None, 28, 28, 128) 1152 \n", 228 | "_________________________________________________________________\n", 229 | "conv_dw_4_bn (BatchNormaliza (None, 28, 28, 128) 512 \n", 230 | "_________________________________________________________________\n", 231 | "conv_dw_4_relu (Activation) (None, 28, 28, 128) 0 \n", 232 | "_________________________________________________________________\n", 233 | "conv_pw_4 (Conv2D) (None, 28, 28, 256) 32768 \n", 234 | "_________________________________________________________________\n", 235 | "conv_pw_4_bn (BatchNormaliza (None, 28, 28, 256) 1024 \n", 236 | "_________________________________________________________________\n", 237 | "conv_pw_4_relu (Activation) (None, 28, 28, 256) 0 \n", 238 | "_________________________________________________________________\n", 239 | "conv_pad_5 (ZeroPadding2D) (None, 30, 30, 256) 0 \n", 240 | "_________________________________________________________________\n", 241 | "conv_dw_5 (DepthwiseConv2D) (None, 28, 28, 256) 2304 \n", 242 | "_________________________________________________________________\n", 243 | "conv_dw_5_bn (BatchNormaliza (None, 28, 28, 256) 1024 \n", 244 | "_________________________________________________________________\n", 245 | "conv_dw_5_relu (Activation) (None, 28, 28, 256) 0 \n", 246 | "_________________________________________________________________\n", 247 | "conv_pw_5 (Conv2D) (None, 28, 28, 256) 65536 \n", 248 | "_________________________________________________________________\n", 249 | "conv_pw_5_bn (BatchNormaliza (None, 28, 28, 256) 1024 \n", 250 | "_________________________________________________________________\n", 251 | "conv_pw_5_relu (Activation) (None, 28, 28, 256) 0 \n", 252 | "_________________________________________________________________\n", 253 | "conv_pad_6 (ZeroPadding2D) (None, 30, 30, 256) 0 \n", 254 | "_________________________________________________________________\n", 255 | "conv_dw_6 (DepthwiseConv2D) (None, 14, 14, 256) 2304 \n", 256 | "_________________________________________________________________\n", 257 | "conv_dw_6_bn (BatchNormaliza (None, 14, 14, 256) 1024 \n", 258 | "_________________________________________________________________\n", 259 | "conv_dw_6_relu (Activation) (None, 14, 14, 256) 0 \n", 260 | "_________________________________________________________________\n", 261 | "conv_pw_6 (Conv2D) (None, 14, 14, 512) 131072 \n", 262 | "_________________________________________________________________\n", 263 | "conv_pw_6_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 264 | "_________________________________________________________________\n", 265 | "conv_pw_6_relu (Activation) (None, 14, 14, 512) 0 \n", 266 | "_________________________________________________________________\n", 267 | "conv_pad_7 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 268 | "_________________________________________________________________\n", 269 | "conv_dw_7 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 270 | "_________________________________________________________________\n", 271 | "conv_dw_7_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 272 | "_________________________________________________________________\n", 273 | "conv_dw_7_relu (Activation) (None, 14, 14, 512) 0 \n", 274 | "_________________________________________________________________\n", 275 | "conv_pw_7 (Conv2D) (None, 14, 14, 512) 262144 \n", 276 | "_________________________________________________________________\n", 277 | "conv_pw_7_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 278 | "_________________________________________________________________\n", 279 | "conv_pw_7_relu (Activation) (None, 14, 14, 512) 0 \n", 280 | "_________________________________________________________________\n", 281 | "conv_pad_8 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 282 | "_________________________________________________________________\n", 283 | "conv_dw_8 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 284 | "_________________________________________________________________\n", 285 | "conv_dw_8_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 286 | "_________________________________________________________________\n", 287 | "conv_dw_8_relu (Activation) (None, 14, 14, 512) 0 \n", 288 | "_________________________________________________________________\n", 289 | "conv_pw_8 (Conv2D) (None, 14, 14, 512) 262144 \n", 290 | "_________________________________________________________________\n", 291 | "conv_pw_8_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 292 | "_________________________________________________________________\n", 293 | "conv_pw_8_relu (Activation) (None, 14, 14, 512) 0 \n", 294 | "_________________________________________________________________\n", 295 | "conv_pad_9 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 296 | "_________________________________________________________________\n", 297 | "conv_dw_9 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 298 | "_________________________________________________________________\n", 299 | "conv_dw_9_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 300 | "_________________________________________________________________\n", 301 | "conv_dw_9_relu (Activation) (None, 14, 14, 512) 0 \n", 302 | "_________________________________________________________________\n", 303 | "conv_pw_9 (Conv2D) (None, 14, 14, 512) 262144 \n", 304 | "_________________________________________________________________\n", 305 | "conv_pw_9_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 306 | "_________________________________________________________________\n", 307 | "conv_pw_9_relu (Activation) (None, 14, 14, 512) 0 \n", 308 | "_________________________________________________________________\n", 309 | "conv_pad_10 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 310 | "_________________________________________________________________\n", 311 | "conv_dw_10 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 312 | "_________________________________________________________________\n", 313 | "conv_dw_10_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 314 | "_________________________________________________________________\n", 315 | "conv_dw_10_relu (Activation) (None, 14, 14, 512) 0 \n", 316 | "_________________________________________________________________\n", 317 | "conv_pw_10 (Conv2D) (None, 14, 14, 512) 262144 \n", 318 | "_________________________________________________________________\n", 319 | "conv_pw_10_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 320 | "_________________________________________________________________\n", 321 | "conv_pw_10_relu (Activation) (None, 14, 14, 512) 0 \n", 322 | "_________________________________________________________________\n", 323 | "conv_pad_11 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 324 | "_________________________________________________________________\n", 325 | "conv_dw_11 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 326 | "_________________________________________________________________\n", 327 | "conv_dw_11_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 328 | "_________________________________________________________________\n", 329 | "conv_dw_11_relu (Activation) (None, 14, 14, 512) 0 \n", 330 | "_________________________________________________________________\n", 331 | "conv_pw_11 (Conv2D) (None, 14, 14, 512) 262144 \n", 332 | "_________________________________________________________________\n", 333 | "conv_pw_11_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 334 | "_________________________________________________________________\n", 335 | "conv_pw_11_relu (Activation) (None, 14, 14, 512) 0 \n", 336 | "_________________________________________________________________\n", 337 | "conv_pad_12 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 338 | "_________________________________________________________________\n", 339 | "conv_dw_12 (DepthwiseConv2D) (None, 7, 7, 512) 4608 \n", 340 | "_________________________________________________________________\n", 341 | "conv_dw_12_bn (BatchNormaliz (None, 7, 7, 512) 2048 \n", 342 | "_________________________________________________________________\n", 343 | "conv_dw_12_relu (Activation) (None, 7, 7, 512) 0 \n", 344 | "_________________________________________________________________\n", 345 | "conv_pw_12 (Conv2D) (None, 7, 7, 1024) 524288 \n", 346 | "_________________________________________________________________\n", 347 | "conv_pw_12_bn (BatchNormaliz (None, 7, 7, 1024) 4096 \n", 348 | "_________________________________________________________________\n", 349 | "conv_pw_12_relu (Activation) (None, 7, 7, 1024) 0 \n", 350 | "_________________________________________________________________\n", 351 | "conv_pad_13 (ZeroPadding2D) (None, 9, 9, 1024) 0 \n", 352 | "_________________________________________________________________\n", 353 | "conv_dw_13 (DepthwiseConv2D) (None, 7, 7, 1024) 9216 \n", 354 | "_________________________________________________________________\n", 355 | "conv_dw_13_bn (BatchNormaliz (None, 7, 7, 1024) 4096 \n", 356 | "_________________________________________________________________\n", 357 | "conv_dw_13_relu (Activation) (None, 7, 7, 1024) 0 \n", 358 | "_________________________________________________________________\n", 359 | "conv_pw_13 (Conv2D) (None, 7, 7, 1024) 1048576 \n", 360 | "_________________________________________________________________\n", 361 | "conv_pw_13_bn (BatchNormaliz (None, 7, 7, 1024) 4096 \n", 362 | "_________________________________________________________________\n", 363 | "conv_pw_13_relu (Activation) (None, 7, 7, 1024) 0 \n", 364 | "=================================================================\n", 365 | "Total params: 3,228,864\n", 366 | "Trainable params: 3,206,976\n", 367 | "Non-trainable params: 21,888\n", 368 | "_________________________________________________________________\n" 369 | ] 370 | } 371 | ], 372 | "source": [ 373 | "def loadModel():\n", 374 | " mobilenet = keras.applications.mobilenet.MobileNet()\n", 375 | " model = Model(inputs=mobilenet.input,outputs=mobilenet.get_layer('conv_pw_13_relu').output)\n", 376 | " return model\n", 377 | "\n", 378 | "mobilenet = loadModel()\n", 379 | "mobilenet.summary()" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "Add classification layer to the top and freeze the other layers." 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 22, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "_________________________________________________________________\n", 399 | "Layer (type) Output Shape Param # \n", 400 | "=================================================================\n", 401 | "input_1 (InputLayer) (None, 224, 224, 3) 0 \n", 402 | "_________________________________________________________________\n", 403 | "conv1_pad (ZeroPadding2D) (None, 226, 226, 3) 0 \n", 404 | "_________________________________________________________________\n", 405 | "conv1 (Conv2D) (None, 112, 112, 32) 864 \n", 406 | "_________________________________________________________________\n", 407 | "conv1_bn (BatchNormalization (None, 112, 112, 32) 128 \n", 408 | "_________________________________________________________________\n", 409 | "conv1_relu (Activation) (None, 112, 112, 32) 0 \n", 410 | "_________________________________________________________________\n", 411 | "conv_pad_1 (ZeroPadding2D) (None, 114, 114, 32) 0 \n", 412 | "_________________________________________________________________\n", 413 | "conv_dw_1 (DepthwiseConv2D) (None, 112, 112, 32) 288 \n", 414 | "_________________________________________________________________\n", 415 | "conv_dw_1_bn (BatchNormaliza (None, 112, 112, 32) 128 \n", 416 | "_________________________________________________________________\n", 417 | "conv_dw_1_relu (Activation) (None, 112, 112, 32) 0 \n", 418 | "_________________________________________________________________\n", 419 | "conv_pw_1 (Conv2D) (None, 112, 112, 64) 2048 \n", 420 | "_________________________________________________________________\n", 421 | "conv_pw_1_bn (BatchNormaliza (None, 112, 112, 64) 256 \n", 422 | "_________________________________________________________________\n", 423 | "conv_pw_1_relu (Activation) (None, 112, 112, 64) 0 \n", 424 | "_________________________________________________________________\n", 425 | "conv_pad_2 (ZeroPadding2D) (None, 114, 114, 64) 0 \n", 426 | "_________________________________________________________________\n", 427 | "conv_dw_2 (DepthwiseConv2D) (None, 56, 56, 64) 576 \n", 428 | "_________________________________________________________________\n", 429 | "conv_dw_2_bn (BatchNormaliza (None, 56, 56, 64) 256 \n", 430 | "_________________________________________________________________\n", 431 | "conv_dw_2_relu (Activation) (None, 56, 56, 64) 0 \n", 432 | "_________________________________________________________________\n", 433 | "conv_pw_2 (Conv2D) (None, 56, 56, 128) 8192 \n", 434 | "_________________________________________________________________\n", 435 | "conv_pw_2_bn (BatchNormaliza (None, 56, 56, 128) 512 \n", 436 | "_________________________________________________________________\n", 437 | "conv_pw_2_relu (Activation) (None, 56, 56, 128) 0 \n", 438 | "_________________________________________________________________\n", 439 | "conv_pad_3 (ZeroPadding2D) (None, 58, 58, 128) 0 \n", 440 | "_________________________________________________________________\n", 441 | "conv_dw_3 (DepthwiseConv2D) (None, 56, 56, 128) 1152 \n", 442 | "_________________________________________________________________\n", 443 | "conv_dw_3_bn (BatchNormaliza (None, 56, 56, 128) 512 \n", 444 | "_________________________________________________________________\n", 445 | "conv_dw_3_relu (Activation) (None, 56, 56, 128) 0 \n", 446 | "_________________________________________________________________\n", 447 | "conv_pw_3 (Conv2D) (None, 56, 56, 128) 16384 \n", 448 | "_________________________________________________________________\n", 449 | "conv_pw_3_bn (BatchNormaliza (None, 56, 56, 128) 512 \n", 450 | "_________________________________________________________________\n", 451 | "conv_pw_3_relu (Activation) (None, 56, 56, 128) 0 \n", 452 | "_________________________________________________________________\n", 453 | "conv_pad_4 (ZeroPadding2D) (None, 58, 58, 128) 0 \n", 454 | "_________________________________________________________________\n", 455 | "conv_dw_4 (DepthwiseConv2D) (None, 28, 28, 128) 1152 \n", 456 | "_________________________________________________________________\n", 457 | "conv_dw_4_bn (BatchNormaliza (None, 28, 28, 128) 512 \n", 458 | "_________________________________________________________________\n", 459 | "conv_dw_4_relu (Activation) (None, 28, 28, 128) 0 \n", 460 | "_________________________________________________________________\n", 461 | "conv_pw_4 (Conv2D) (None, 28, 28, 256) 32768 \n", 462 | "_________________________________________________________________\n", 463 | "conv_pw_4_bn (BatchNormaliza (None, 28, 28, 256) 1024 \n", 464 | "_________________________________________________________________\n", 465 | "conv_pw_4_relu (Activation) (None, 28, 28, 256) 0 \n", 466 | "_________________________________________________________________\n", 467 | "conv_pad_5 (ZeroPadding2D) (None, 30, 30, 256) 0 \n", 468 | "_________________________________________________________________\n", 469 | "conv_dw_5 (DepthwiseConv2D) (None, 28, 28, 256) 2304 \n", 470 | "_________________________________________________________________\n", 471 | "conv_dw_5_bn (BatchNormaliza (None, 28, 28, 256) 1024 \n", 472 | "_________________________________________________________________\n", 473 | "conv_dw_5_relu (Activation) (None, 28, 28, 256) 0 \n", 474 | "_________________________________________________________________\n", 475 | "conv_pw_5 (Conv2D) (None, 28, 28, 256) 65536 \n", 476 | "_________________________________________________________________\n", 477 | "conv_pw_5_bn (BatchNormaliza (None, 28, 28, 256) 1024 \n", 478 | "_________________________________________________________________\n", 479 | "conv_pw_5_relu (Activation) (None, 28, 28, 256) 0 \n", 480 | "_________________________________________________________________\n", 481 | "conv_pad_6 (ZeroPadding2D) (None, 30, 30, 256) 0 \n", 482 | "_________________________________________________________________\n", 483 | "conv_dw_6 (DepthwiseConv2D) (None, 14, 14, 256) 2304 \n", 484 | "_________________________________________________________________\n", 485 | "conv_dw_6_bn (BatchNormaliza (None, 14, 14, 256) 1024 \n", 486 | "_________________________________________________________________\n", 487 | "conv_dw_6_relu (Activation) (None, 14, 14, 256) 0 \n", 488 | "_________________________________________________________________\n", 489 | "conv_pw_6 (Conv2D) (None, 14, 14, 512) 131072 \n", 490 | "_________________________________________________________________\n", 491 | "conv_pw_6_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 492 | "_________________________________________________________________\n", 493 | "conv_pw_6_relu (Activation) (None, 14, 14, 512) 0 \n", 494 | "_________________________________________________________________\n", 495 | "conv_pad_7 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 496 | "_________________________________________________________________\n", 497 | "conv_dw_7 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 498 | "_________________________________________________________________\n", 499 | "conv_dw_7_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 500 | "_________________________________________________________________\n", 501 | "conv_dw_7_relu (Activation) (None, 14, 14, 512) 0 \n", 502 | "_________________________________________________________________\n", 503 | "conv_pw_7 (Conv2D) (None, 14, 14, 512) 262144 \n", 504 | "_________________________________________________________________\n", 505 | "conv_pw_7_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 506 | "_________________________________________________________________\n", 507 | "conv_pw_7_relu (Activation) (None, 14, 14, 512) 0 \n", 508 | "_________________________________________________________________\n", 509 | "conv_pad_8 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 510 | "_________________________________________________________________\n", 511 | "conv_dw_8 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 512 | "_________________________________________________________________\n", 513 | "conv_dw_8_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 514 | "_________________________________________________________________\n", 515 | "conv_dw_8_relu (Activation) (None, 14, 14, 512) 0 \n", 516 | "_________________________________________________________________\n", 517 | "conv_pw_8 (Conv2D) (None, 14, 14, 512) 262144 \n", 518 | "_________________________________________________________________\n", 519 | "conv_pw_8_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 520 | "_________________________________________________________________\n", 521 | "conv_pw_8_relu (Activation) (None, 14, 14, 512) 0 \n", 522 | "_________________________________________________________________\n", 523 | "conv_pad_9 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 524 | "_________________________________________________________________\n", 525 | "conv_dw_9 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 526 | "_________________________________________________________________\n", 527 | "conv_dw_9_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 528 | "_________________________________________________________________\n", 529 | "conv_dw_9_relu (Activation) (None, 14, 14, 512) 0 \n", 530 | "_________________________________________________________________\n", 531 | "conv_pw_9 (Conv2D) (None, 14, 14, 512) 262144 \n", 532 | "_________________________________________________________________\n", 533 | "conv_pw_9_bn (BatchNormaliza (None, 14, 14, 512) 2048 \n", 534 | "_________________________________________________________________\n", 535 | "conv_pw_9_relu (Activation) (None, 14, 14, 512) 0 \n", 536 | "_________________________________________________________________\n", 537 | "conv_pad_10 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 538 | "_________________________________________________________________\n", 539 | "conv_dw_10 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 540 | "_________________________________________________________________\n", 541 | "conv_dw_10_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 542 | "_________________________________________________________________\n", 543 | "conv_dw_10_relu (Activation) (None, 14, 14, 512) 0 \n", 544 | "_________________________________________________________________\n", 545 | "conv_pw_10 (Conv2D) (None, 14, 14, 512) 262144 \n", 546 | "_________________________________________________________________\n", 547 | "conv_pw_10_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 548 | "_________________________________________________________________\n", 549 | "conv_pw_10_relu (Activation) (None, 14, 14, 512) 0 \n", 550 | "_________________________________________________________________\n", 551 | "conv_pad_11 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 552 | "_________________________________________________________________\n", 553 | "conv_dw_11 (DepthwiseConv2D) (None, 14, 14, 512) 4608 \n", 554 | "_________________________________________________________________\n", 555 | "conv_dw_11_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 556 | "_________________________________________________________________\n", 557 | "conv_dw_11_relu (Activation) (None, 14, 14, 512) 0 \n", 558 | "_________________________________________________________________\n", 559 | "conv_pw_11 (Conv2D) (None, 14, 14, 512) 262144 \n", 560 | "_________________________________________________________________\n", 561 | "conv_pw_11_bn (BatchNormaliz (None, 14, 14, 512) 2048 \n", 562 | "_________________________________________________________________\n", 563 | "conv_pw_11_relu (Activation) (None, 14, 14, 512) 0 \n", 564 | "_________________________________________________________________\n", 565 | "conv_pad_12 (ZeroPadding2D) (None, 16, 16, 512) 0 \n", 566 | "_________________________________________________________________\n", 567 | "conv_dw_12 (DepthwiseConv2D) (None, 7, 7, 512) 4608 \n", 568 | "_________________________________________________________________\n", 569 | "conv_dw_12_bn (BatchNormaliz (None, 7, 7, 512) 2048 \n", 570 | "_________________________________________________________________\n", 571 | "conv_dw_12_relu (Activation) (None, 7, 7, 512) 0 \n", 572 | "_________________________________________________________________\n", 573 | "conv_pw_12 (Conv2D) (None, 7, 7, 1024) 524288 \n", 574 | "_________________________________________________________________\n", 575 | "conv_pw_12_bn (BatchNormaliz (None, 7, 7, 1024) 4096 \n", 576 | "_________________________________________________________________\n", 577 | "conv_pw_12_relu (Activation) (None, 7, 7, 1024) 0 \n", 578 | "_________________________________________________________________\n", 579 | "conv_pad_13 (ZeroPadding2D) (None, 9, 9, 1024) 0 \n", 580 | "_________________________________________________________________\n", 581 | "conv_dw_13 (DepthwiseConv2D) (None, 7, 7, 1024) 9216 \n", 582 | "_________________________________________________________________\n", 583 | "conv_dw_13_bn (BatchNormaliz (None, 7, 7, 1024) 4096 \n", 584 | "_________________________________________________________________\n", 585 | "conv_dw_13_relu (Activation) (None, 7, 7, 1024) 0 \n", 586 | "_________________________________________________________________\n", 587 | "conv_pw_13 (Conv2D) (None, 7, 7, 1024) 1048576 \n", 588 | "_________________________________________________________________\n", 589 | "conv_pw_13_bn (BatchNormaliz (None, 7, 7, 1024) 4096 \n", 590 | "_________________________________________________________________\n", 591 | "conv_pw_13_relu (Activation) (None, 7, 7, 1024) 0 \n", 592 | "_________________________________________________________________\n", 593 | "flatten_13 (Flatten) (None, 50176) 0 \n", 594 | "_________________________________________________________________\n", 595 | "dense_17 (Dense) (None, 100) 5017700 \n", 596 | "_________________________________________________________________\n", 597 | "dense_18 (Dense) (None, 3) 303 \n", 598 | "=================================================================\n", 599 | "Total params: 8,246,867\n", 600 | "Trainable params: 5,018,003\n", 601 | "Non-trainable params: 3,228,864\n", 602 | "_________________________________________________________________\n" 603 | ] 604 | } 605 | ], 606 | "source": [ 607 | "# make a new softmax layer with num_classes neurons\n", 608 | "flatten = Flatten(input_shape=(7,7,1024))(mobilenet.layers[-1].output)\n", 609 | "fc = Dense(100, activation='relu')(flatten)\n", 610 | "output = Dense(num_classes, activation='softmax')(fc)\n", 611 | "\n", 612 | "model = Model(mobilenet.input, output)\n", 613 | "\n", 614 | "\n", 615 | "# make all layers untrainable by freezing weights (except for last two layers)\n", 616 | "for l, layer in enumerate(model.layers[:-2]):\n", 617 | " layer.trainable = False\n", 618 | "\n", 619 | "# ensure the last layer is trainable/not frozen\n", 620 | "for l, layer in enumerate(model.layers[-2:]):\n", 621 | " layer.trainable = True\n", 622 | "\n", 623 | "model.summary()" 624 | ] 625 | }, 626 | { 627 | "cell_type": "markdown", 628 | "metadata": {}, 629 | "source": [ 630 | "Let's train:" 631 | ] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "execution_count": 23, 636 | "metadata": {}, 637 | "outputs": [ 638 | { 639 | "name": "stdout", 640 | "output_type": "stream", 641 | "text": [ 642 | "Epoch 1/10\n" 643 | ] 644 | }, 645 | { 646 | "ename": "ResourceExhaustedError", 647 | "evalue": "OOM when allocating tensor with shape[50176,1000] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc\n\t [[Node: dense_11/random_uniform/RandomUniform = RandomUniform[T=DT_INT32, dtype=DT_FLOAT, seed=87654321, seed2=339866, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"](dense_11/random_uniform/shape)]]\nHint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.\n\n\nCaused by op 'dense_11/random_uniform/RandomUniform', defined at:\n File \"/usr/lib/python3.5/runpy.py\", line 184, in _run_module_as_main\n \"__main__\", mod_spec)\n File \"/usr/lib/python3.5/runpy.py\", line 85, in _run_code\n exec(code, run_globals)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py\", line 16, in \n app.launch_new_instance()\n File \"/home/gene/.local/lib/python3.5/site-packages/traitlets/config/application.py\", line 658, in launch_instance\n app.start()\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelapp.py\", line 486, in start\n self.io_loop.start()\n File \"/usr/local/lib/python3.5/dist-packages/tornado/ioloop.py\", line 888, in start\n handler_func(fd_obj, events)\n File \"/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py\", line 277, in null_wrapper\n return fn(*args, **kwargs)\n File \"/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py\", line 450, in _handle_events\n self._handle_recv()\n File \"/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py\", line 480, in _handle_recv\n self._run_callback(callback, msg)\n File \"/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py\", line 432, in _run_callback\n callback(*args, **kwargs)\n File \"/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py\", line 277, in null_wrapper\n return fn(*args, **kwargs)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\", line 283, in dispatcher\n return self.dispatch_shell(stream, msg)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\", line 233, in dispatch_shell\n handler(stream, idents, msg)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\", line 399, in execute_request\n user_expressions, allow_stdin)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/ipkernel.py\", line 208, in do_execute\n res = shell.run_cell(code, store_history=store_history, silent=silent)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py\", line 537, in run_cell\n return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)\n File \"/home/gene/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2728, in run_cell\n interactivity=interactivity, compiler=compiler, result=result)\n File \"/home/gene/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2850, in run_ast_nodes\n if self.run_code(code, result):\n File \"/home/gene/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2910, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"\", line 5, in \n fc = Dense(1000, activation='relu')(flatten)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/engine/base_layer.py\", line 432, in __call__\n self.build(input_shapes[0])\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/layers/core.py\", line 872, in build\n constraint=self.kernel_constraint)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py\", line 91, in wrapper\n return func(*args, **kwargs)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/engine/base_layer.py\", line 249, in add_weight\n weight = K.variable(initializer(shape),\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/initializers.py\", line 218, in __call__\n dtype=dtype, seed=self.seed)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py\", line 4077, in random_uniform\n dtype=dtype, seed=seed)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/ops/random_ops.py\", line 242, in random_uniform\n rnd = gen_random_ops.random_uniform(shape, dtype, seed=seed1, seed2=seed2)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_random_ops.py\", line 674, in random_uniform\n name=name)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py\", line 787, in _apply_op_helper\n op_def=op_def)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py\", line 3392, in create_op\n op_def=op_def)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py\", line 1718, in __init__\n self._traceback = self._graph._extract_stack() # pylint: disable=protected-access\n\nResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[50176,1000] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc\n\t [[Node: dense_11/random_uniform/RandomUniform = RandomUniform[T=DT_INT32, dtype=DT_FLOAT, seed=87654321, seed2=339866, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"](dense_11/random_uniform/shape)]]\nHint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.\n\n", 648 | "output_type": "error", 649 | "traceback": [ 650 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 651 | "\u001b[0;31mResourceExhaustedError\u001b[0m Traceback (most recent call last)", 652 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_do_call\u001b[0;34m(self, fn, *args)\u001b[0m\n\u001b[1;32m 1321\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1322\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1323\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 653 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[0;34m(feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[1;32m 1306\u001b[0m return self._call_tf_sessionrun(\n\u001b[0;32m-> 1307\u001b[0;31m options, feed_dict, fetch_list, target_list, run_metadata)\n\u001b[0m\u001b[1;32m 1308\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 654 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_call_tf_sessionrun\u001b[0;34m(self, options, feed_dict, fetch_list, target_list, run_metadata)\u001b[0m\n\u001b[1;32m 1408\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moptions\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1409\u001b[0;31m run_metadata)\n\u001b[0m\u001b[1;32m 1410\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 655 | "\u001b[0;31mResourceExhaustedError\u001b[0m: OOM when allocating tensor with shape[50176,1000] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc\n\t [[Node: dense_11/random_uniform/RandomUniform = RandomUniform[T=DT_INT32, dtype=DT_FLOAT, seed=87654321, seed2=339866, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"](dense_11/random_uniform/shape)]]\nHint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.\n", 656 | "\nDuring handling of the above exception, another exception occurred:\n", 657 | "\u001b[0;31mResourceExhaustedError\u001b[0m Traceback (most recent call last)", 658 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moptimizer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'adam'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0;34m'categorical_crossentropy'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmetrics\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'accuracy'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m16\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 659 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/keras/engine/training.py\u001b[0m in \u001b[0;36mfit\u001b[0;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, **kwargs)\u001b[0m\n\u001b[1;32m 1040\u001b[0m \u001b[0minitial_epoch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minitial_epoch\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1041\u001b[0m \u001b[0msteps_per_epoch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msteps_per_epoch\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1042\u001b[0;31m validation_steps=validation_steps)\n\u001b[0m\u001b[1;32m 1043\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1044\u001b[0m def evaluate(self, x=None, y=None,\n", 660 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/keras/engine/training_arrays.py\u001b[0m in \u001b[0;36mfit_loop\u001b[0;34m(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)\u001b[0m\n\u001b[1;32m 197\u001b[0m \u001b[0mins_batch\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mins_batch\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtoarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 198\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 199\u001b[0;31m \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mins_batch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 200\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mouts\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mouts\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 661 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m 2651\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2652\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2653\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mget_session\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'_make_callable_from_options'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2654\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpy_any\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mis_sparse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2655\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpy_any\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mis_tensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 662 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py\u001b[0m in \u001b[0;36mget_session\u001b[0;34m()\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_keras_initialized\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0muninitialized_vars\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 203\u001b[0;31m \u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvariables_initializer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muninitialized_vars\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 204\u001b[0m \u001b[0;31m# hack for list_devices() function.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 205\u001b[0m \u001b[0;31m# list_devices() function is not available under tensorflow r1.3.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 663 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m 898\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 899\u001b[0m result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[0;32m--> 900\u001b[0;31m run_metadata_ptr)\n\u001b[0m\u001b[1;32m 901\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 902\u001b[0m \u001b[0mproto_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 664 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m 1133\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1134\u001b[0m results = self._do_run(handle, final_targets, final_fetches,\n\u001b[0;32m-> 1135\u001b[0;31m feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[1;32m 1136\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1137\u001b[0m \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 665 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_do_run\u001b[0;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m 1314\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1315\u001b[0m return self._do_call(_run_fn, feeds, fetches, targets, options,\n\u001b[0;32m-> 1316\u001b[0;31m run_metadata)\n\u001b[0m\u001b[1;32m 1317\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1318\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 666 | "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_do_call\u001b[0;34m(self, fn, *args)\u001b[0m\n\u001b[1;32m 1333\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1334\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1335\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode_def\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1336\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1337\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_extend_graph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 667 | "\u001b[0;31mResourceExhaustedError\u001b[0m: OOM when allocating tensor with shape[50176,1000] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc\n\t [[Node: dense_11/random_uniform/RandomUniform = RandomUniform[T=DT_INT32, dtype=DT_FLOAT, seed=87654321, seed2=339866, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"](dense_11/random_uniform/shape)]]\nHint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.\n\n\nCaused by op 'dense_11/random_uniform/RandomUniform', defined at:\n File \"/usr/lib/python3.5/runpy.py\", line 184, in _run_module_as_main\n \"__main__\", mod_spec)\n File \"/usr/lib/python3.5/runpy.py\", line 85, in _run_code\n exec(code, run_globals)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py\", line 16, in \n app.launch_new_instance()\n File \"/home/gene/.local/lib/python3.5/site-packages/traitlets/config/application.py\", line 658, in launch_instance\n app.start()\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelapp.py\", line 486, in start\n self.io_loop.start()\n File \"/usr/local/lib/python3.5/dist-packages/tornado/ioloop.py\", line 888, in start\n handler_func(fd_obj, events)\n File \"/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py\", line 277, in null_wrapper\n return fn(*args, **kwargs)\n File \"/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py\", line 450, in _handle_events\n self._handle_recv()\n File \"/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py\", line 480, in _handle_recv\n self._run_callback(callback, msg)\n File \"/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py\", line 432, in _run_callback\n callback(*args, **kwargs)\n File \"/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py\", line 277, in null_wrapper\n return fn(*args, **kwargs)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\", line 283, in dispatcher\n return self.dispatch_shell(stream, msg)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\", line 233, in dispatch_shell\n handler(stream, idents, msg)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\", line 399, in execute_request\n user_expressions, allow_stdin)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/ipkernel.py\", line 208, in do_execute\n res = shell.run_cell(code, store_history=store_history, silent=silent)\n File \"/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py\", line 537, in run_cell\n return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)\n File \"/home/gene/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2728, in run_cell\n interactivity=interactivity, compiler=compiler, result=result)\n File \"/home/gene/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2850, in run_ast_nodes\n if self.run_code(code, result):\n File \"/home/gene/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2910, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"\", line 5, in \n fc = Dense(1000, activation='relu')(flatten)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/engine/base_layer.py\", line 432, in __call__\n self.build(input_shapes[0])\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/layers/core.py\", line 872, in build\n constraint=self.kernel_constraint)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py\", line 91, in wrapper\n return func(*args, **kwargs)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/engine/base_layer.py\", line 249, in add_weight\n weight = K.variable(initializer(shape),\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/initializers.py\", line 218, in __call__\n dtype=dtype, seed=self.seed)\n File \"/home/gene/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py\", line 4077, in random_uniform\n dtype=dtype, seed=seed)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/ops/random_ops.py\", line 242, in random_uniform\n rnd = gen_random_ops.random_uniform(shape, dtype, seed=seed1, seed2=seed2)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_random_ops.py\", line 674, in random_uniform\n name=name)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py\", line 787, in _apply_op_helper\n op_def=op_def)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py\", line 3392, in create_op\n op_def=op_def)\n File \"/home/gene/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py\", line 1718, in __init__\n self._traceback = self._graph._extract_stack() # pylint: disable=protected-access\n\nResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[50176,1000] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc\n\t [[Node: dense_11/random_uniform/RandomUniform = RandomUniform[T=DT_INT32, dtype=DT_FLOAT, seed=87654321, seed2=339866, _device=\"/job:localhost/replica:0/task:0/device:GPU:0\"](dense_11/random_uniform/shape)]]\nHint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.\n\n" 668 | ] 669 | } 670 | ], 671 | "source": [ 672 | "model.compile(optimizer='adam', loss= 'categorical_crossentropy', metrics=['accuracy'])\n", 673 | "model.fit(x_train, y_train, epochs=10, batch_size=16)\n" 674 | ] 675 | }, 676 | { 677 | "cell_type": "code", 678 | "execution_count": null, 679 | "metadata": {}, 680 | "outputs": [], 681 | "source": [] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": null, 686 | "metadata": {}, 687 | "outputs": [], 688 | "source": [] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": 6, 693 | "metadata": {}, 694 | "outputs": [ 695 | { 696 | "name": "stdout", 697 | "output_type": "stream", 698 | "text": [ 699 | "('Test loss:', 0.280648006429334)\n", 700 | "('Test accuracy:', 0.952380947650425)\n" 701 | ] 702 | } 703 | ], 704 | "source": [ 705 | "loss, accuracy = model.evaluate(x_test, y_test, verbose=0)\n", 706 | "print('Test loss:', loss)\n", 707 | "print('Test accuracy:', accuracy)" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 29, 713 | "metadata": {}, 714 | "outputs": [ 715 | { 716 | "name": "stdout", 717 | "output_type": "stream", 718 | "text": [ 719 | "predicted 1 (actual 1) in 0.219\n" 720 | ] 721 | } 722 | ], 723 | "source": [ 724 | "\n", 725 | "import time\n", 726 | "\n", 727 | "\n", 728 | "\n", 729 | "t1 = time.time()\n", 730 | "idx = int(50*random.random())\n", 731 | "y_prob = model.predict(x_test[idx].reshape((1,224,224,3)))\n", 732 | "actual = np.argmax(y_test[idx])\n", 733 | "prediction = np.argmax(y_prob)\n", 734 | "print(\"predicted %d (actual %d) in %0.3f\" % (prediction, actual, (time.time()-t1)))\n" 735 | ] 736 | }, 737 | { 738 | "cell_type": "code", 739 | "execution_count": 27, 740 | "metadata": {}, 741 | "outputs": [], 742 | "source": [ 743 | "from keras.models import load_model\n", 744 | "\n", 745 | "model.save('my_model.h5') # creates a HDF5 file 'my_model.h5'\n", 746 | "del model # deletes the existing model\n", 747 | "\n", 748 | "# loads a compiled model \n", 749 | "#model = load_model('my_model.h5')" 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "execution_count": 80, 755 | "metadata": {}, 756 | "outputs": [ 757 | { 758 | "name": "stdout", 759 | "output_type": "stream", 760 | "text": [ 761 | "(1, 1, 0)\n", 762 | "(1, 1, 0)\n", 763 | "(1, 1, 0)\n", 764 | "(0, 1, 1)\n", 765 | "(1, 1, 0)\n", 766 | "(1, 1, 0)\n", 767 | "(0, 0, 0)\n", 768 | "(1, 1, 0)\n", 769 | "(1, 1, 0)\n", 770 | "(1, 0, 1)\n", 771 | "(1, 1, 0)\n", 772 | "(1, 0, 1)\n", 773 | "(1, 1, 0)\n", 774 | "(1, 1, 0)\n", 775 | "(1, 1, 0)\n", 776 | "(1, 1, 0)\n", 777 | "(1, 1, 0)\n", 778 | "(1, 1, 0)\n", 779 | "(0, 0, 0)\n", 780 | "(1, 1, 0)\n", 781 | "(1, 1, 0)\n", 782 | "(1, 1, 0)\n", 783 | "(1, 1, 0)\n", 784 | "(1, 1, 0)\n", 785 | "(1, 1, 0)\n", 786 | "(1, 1, 0)\n", 787 | "(1, 1, 0)\n", 788 | "(1, 1, 0)\n", 789 | "(1, 1, 0)\n", 790 | "(1, 1, 0)\n", 791 | "(1, 1, 0)\n", 792 | "(1, 1, 0)\n", 793 | "(0, 0, 0)\n", 794 | "(1, 1, 0)\n", 795 | "(0, 0, 0)\n", 796 | "(1, 1, 0)\n", 797 | "(1, 1, 0)\n", 798 | "(0, 1, 1)\n", 799 | "(1, 1, 0)\n", 800 | "(1, 1, 0)\n", 801 | "(1, 1, 0)\n", 802 | "(0, 0, 0)\n", 803 | "(1, 1, 0)\n", 804 | "(0, 0, 0)\n", 805 | "(1, 1, 0)\n", 806 | "(1, 1, 0)\n", 807 | "(1, 1, 0)\n", 808 | "(1, 1, 0)\n", 809 | "(1, 1, 0)\n", 810 | "(1, 1, 0)\n", 811 | "(1, 1, 0)\n", 812 | "(1, 1, 0)\n", 813 | "(0, 0, 0)\n", 814 | "(1, 1, 0)\n", 815 | "(1, 1, 0)\n", 816 | "(1, 1, 0)\n", 817 | "(1, 1, 0)\n", 818 | "(1, 1, 0)\n", 819 | "(1, 1, 0)\n", 820 | "(1, 1, 0)\n", 821 | "(1, 1, 0)\n", 822 | "(1, 1, 0)\n", 823 | "(1, 1, 0)\n", 824 | "(1, 1, 0)\n", 825 | "(1, 1, 0)\n", 826 | "(1, 1, 0)\n", 827 | "(1, 1, 0)\n", 828 | "(1, 1, 0)\n", 829 | "(1, 1, 0)\n", 830 | "(1, 1, 0)\n", 831 | "(1, 1, 0)\n", 832 | "(1, 1, 0)\n", 833 | "(0, 0, 0)\n", 834 | "(1, 1, 0)\n", 835 | "(1, 1, 0)\n", 836 | "(0, 0, 0)\n", 837 | "(1, 1, 0)\n", 838 | "(1, 1, 0)\n", 839 | "(1, 1, 0)\n", 840 | "(0, 0, 0)\n", 841 | "(0, 0, 0)\n", 842 | "(1, 1, 0)\n", 843 | "(0, 0, 0)\n", 844 | "(1, 1, 0)\n", 845 | "(1, 1, 0)\n", 846 | "(1, 1, 0)\n", 847 | "(1, 1, 0)\n", 848 | "(1, 1, 0)\n", 849 | "(1, 1, 0)\n", 850 | "(1, 1, 0)\n", 851 | "(1, 1, 0)\n", 852 | "(1, 0, 1)\n", 853 | "(1, 1, 0)\n", 854 | "(1, 1, 0)\n", 855 | "(1, 1, 0)\n", 856 | "(1, 1, 0)\n", 857 | "(1, 1, 0)\n", 858 | "(0, 0, 0)\n", 859 | "(1, 1, 0)\n", 860 | "(1, 1, 0)\n", 861 | "(1, 1, 0)\n", 862 | "(0, 0, 0)\n", 863 | "(0, 0, 0)\n", 864 | "(0, 0, 0)\n", 865 | "(1, 1, 0)\n", 866 | "(1, 1, 0)\n", 867 | "(1, 1, 0)\n", 868 | "(1, 1, 0)\n", 869 | "(1, 1, 0)\n", 870 | "(0, 0, 0)\n", 871 | "(1, 1, 0)\n", 872 | "(1, 1, 0)\n", 873 | "(1, 1, 0)\n", 874 | "(1, 1, 0)\n", 875 | "(0, 0, 0)\n", 876 | "(1, 1, 0)\n", 877 | "(0, 0, 0)\n", 878 | "(1, 1, 0)\n", 879 | "(1, 1, 0)\n", 880 | "(1, 1, 0)\n", 881 | "(1, 1, 0)\n", 882 | "(1, 1, 0)\n", 883 | "(1, 1, 0)\n", 884 | "(0, 0, 0)\n", 885 | "(1, 0, 1)\n", 886 | "(1, 1, 0)\n" 887 | ] 888 | } 889 | ], 890 | "source": [ 891 | "for p, a in zip(y_pred, y_test):\n", 892 | " print(np.argmax(p), np.argmax(a), np.abs(np.argmax(p)-np.argmax(a)))" 893 | ] 894 | }, 895 | { 896 | "cell_type": "code", 897 | "execution_count": null, 898 | "metadata": {}, 899 | "outputs": [], 900 | "source": [] 901 | } 902 | ], 903 | "metadata": { 904 | "kernelspec": { 905 | "display_name": "Python 3", 906 | "language": "python", 907 | "name": "python3" 908 | }, 909 | "language_info": { 910 | "codemirror_mode": { 911 | "name": "ipython", 912 | "version": 3 913 | }, 914 | "file_extension": ".py", 915 | "mimetype": "text/x-python", 916 | "name": "python", 917 | "nbconvert_exporter": "python", 918 | "pygments_lexer": "ipython3", 919 | "version": "3.5.2" 920 | } 921 | }, 922 | "nbformat": 4, 923 | "nbformat_minor": 2 924 | } 925 | --------------------------------------------------------------------------------