Created
January 15, 2018 01:05
-
-
Save harpiechoise/009a4032695c94e95b8dc7beef3eef9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using TensorFlow backend.\n" | |
] | |
} | |
], | |
"source": [ | |
"from __future__ import print_function\n", | |
"import numpy as np\n", | |
"from keras.datasets import cifar10\n", | |
"from keras.models import Sequential\n", | |
"from keras.layers import Conv2D,Flatten, Activation,MaxPooling2D,Dense\n", | |
"from keras.optimizers import RMSprop\n", | |
"from keras.utils import np_utils\n", | |
"from keras.layers import Dropout" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"IMG_CHANELS = 3 # Los colores de la imagen en este car RGB son 3 colores\n", | |
"IMG_ROWS = 32 # Largo de la imagen 32x32\n", | |
"IMG_COLS = 32 # Ancho de la imagen 32x32\n", | |
"\n", | |
"#Constantes\n", | |
"BATCH_SIZE = 128\n", | |
"NB_EPOCHS = 20\n", | |
"NB_CLASSES = 10\n", | |
"VERBOSE = 1\n", | |
"VALIDATION_SPLIT = 0.2\n", | |
"OPIMIZER = RMSprop()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Dataset\n", | |
"(X_train,y_train),(X_test,y_test) = cifar10.load_data()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Formato del X_Train: (50000, 32, 32, 3)\n", | |
"Tamaño del X_Train 50000\n", | |
"Formato del X_test (10000, 32, 32, 3)\n", | |
"Tamaño del X_test 10000\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Formato del X_Train: \",X_train.shape)\n", | |
"print(\"Tamaño del X_Train\",X_train.shape[0])\n", | |
"print(\"Formato del X_test\",X_test.shape)\n", | |
"print(\"Tamaño del X_test\",X_test.shape[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#De numeros a one hot\n", | |
"Y_train = np_utils.to_categorical(y_train,NB_CLASSES)\n", | |
"Y_test = np_utils.to_categorical(y_test,NB_CLASSES)\n", | |
"#Normalizacion y etiquetado\n", | |
"X_train = X_train.astype('float32')\n", | |
"X_test = X_test.astype('float32')\n", | |
"X_train /= 255\n", | |
"X_test /= 255" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"conv2d_1 (Conv2D) (None, 32, 32, 32) 896 \n", | |
"_________________________________________________________________\n", | |
"activation_1 (Activation) (None, 32, 32, 32) 0 \n", | |
"_________________________________________________________________\n", | |
"max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32) 0 \n", | |
"_________________________________________________________________\n", | |
"dropout_1 (Dropout) (None, 16, 16, 32) 0 \n", | |
"_________________________________________________________________\n", | |
"flatten_1 (Flatten) (None, 8192) 0 \n", | |
"_________________________________________________________________\n", | |
"dense_1 (Dense) (None, 10) 81930 \n", | |
"_________________________________________________________________\n", | |
"activation_2 (Activation) (None, 10) 0 \n", | |
"=================================================================\n", | |
"Total params: 82,826\n", | |
"Trainable params: 82,826\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"#Red\n", | |
"model = Sequential()\n", | |
"model.add(Conv2D(32, kernel_size=3,padding='same',\n", | |
" input_shape=(IMG_ROWS,IMG_COLS,IMG_CHANELS)))\n", | |
"model.add(Activation('relu'))\n", | |
"model.add(MaxPooling2D(pool_size=(2,2)))\n", | |
"model.add(Dropout(.25))\n", | |
"model.add(Flatten())\n", | |
"model.add(Dense(NB_CLASSES))\n", | |
"model.add(Activation('softmax'))\n", | |
"model.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Train on 40000 samples, validate on 10000 samples\n", | |
"Epoch 1/20\n", | |
"40000/40000 [==============================] - 34s 845us/step - loss: 1.6893 - acc: 0.4082 - val_loss: 1.5426 - val_acc: 0.4617\n", | |
"Epoch 2/20\n", | |
"40000/40000 [==============================] - 31s 764us/step - loss: 1.4003 - acc: 0.5152 - val_loss: 1.3866 - val_acc: 0.5228\n", | |
"Epoch 3/20\n", | |
"40000/40000 [==============================] - 32s 806us/step - loss: 1.2929 - acc: 0.5518 - val_loss: 1.3567 - val_acc: 0.5334\n", | |
"Epoch 4/20\n", | |
"40000/40000 [==============================] - 33s 815us/step - loss: 1.2314 - acc: 0.5727 - val_loss: 1.3008 - val_acc: 0.5383\n", | |
"Epoch 5/20\n", | |
"40000/40000 [==============================] - 33s 837us/step - loss: 1.1876 - acc: 0.5898 - val_loss: 1.2161 - val_acc: 0.5788\n", | |
"Epoch 6/20\n", | |
"40000/40000 [==============================] - 33s 834us/step - loss: 1.1552 - acc: 0.5986 - val_loss: 1.2163 - val_acc: 0.5816\n", | |
"Epoch 7/20\n", | |
"40000/40000 [==============================] - 34s 840us/step - loss: 1.1254 - acc: 0.6101 - val_loss: 1.3196 - val_acc: 0.5438\n", | |
"Epoch 8/20\n", | |
"40000/40000 [==============================] - 35s 881us/step - loss: 1.1005 - acc: 0.6184 - val_loss: 1.1780 - val_acc: 0.5934\n", | |
"Epoch 9/20\n", | |
"40000/40000 [==============================] - 39s 980us/step - loss: 1.0804 - acc: 0.6282 - val_loss: 1.2367 - val_acc: 0.5700\n", | |
"Epoch 10/20\n", | |
"40000/40000 [==============================] - 38s 947us/step - loss: 1.0619 - acc: 0.6346 - val_loss: 1.2038 - val_acc: 0.5821\n", | |
"Epoch 11/20\n", | |
"40000/40000 [==============================] - 41s 1ms/step - loss: 1.0429 - acc: 0.6405 - val_loss: 1.2641 - val_acc: 0.5654\n", | |
"Epoch 12/20\n", | |
"40000/40000 [==============================] - 39s 978us/step - loss: 1.0321 - acc: 0.6427 - val_loss: 1.1748 - val_acc: 0.5902\n", | |
"Epoch 13/20\n", | |
"40000/40000 [==============================] - 40s 990us/step - loss: 1.0165 - acc: 0.6502 - val_loss: 1.2047 - val_acc: 0.5834\n", | |
"Epoch 14/20\n", | |
"40000/40000 [==============================] - 39s 978us/step - loss: 1.0023 - acc: 0.6542 - val_loss: 1.1386 - val_acc: 0.6053\n", | |
"Epoch 15/20\n", | |
"40000/40000 [==============================] - 39s 975us/step - loss: 0.9910 - acc: 0.6593 - val_loss: 1.0925 - val_acc: 0.6257\n", | |
"Epoch 16/20\n", | |
"40000/40000 [==============================] - 41s 1ms/step - loss: 0.9761 - acc: 0.6628 - val_loss: 1.1304 - val_acc: 0.6117\n", | |
"Epoch 17/20\n", | |
"40000/40000 [==============================] - 39s 974us/step - loss: 0.9669 - acc: 0.6653 - val_loss: 1.1060 - val_acc: 0.6246\n", | |
"Epoch 18/20\n", | |
"40000/40000 [==============================] - 41s 1ms/step - loss: 0.9552 - acc: 0.6712 - val_loss: 1.2358 - val_acc: 0.5839\n", | |
"Epoch 19/20\n", | |
"40000/40000 [==============================] - 39s 972us/step - loss: 0.9461 - acc: 0.6750 - val_loss: 1.1343 - val_acc: 0.6080\n", | |
"Epoch 20/20\n", | |
"40000/40000 [==============================] - 39s 982us/step - loss: 0.9352 - acc: 0.6777 - val_loss: 1.1561 - val_acc: 0.5994\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f9875fb2eb8>" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Entrenar\n", | |
"model.compile(loss='categorical_crossentropy',\n", | |
" optimizer=OPIMIZER,\n", | |
" metrics=['accuracy'])\n", | |
"\n", | |
"model.fit(X_train,Y_train,batch_size=BATCH_SIZE,\n", | |
" epochs=NB_EPOCHS,\n", | |
" validation_split=VALIDATION_SPLIT,\n", | |
" verbose = VERBOSE)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10000/10000 [==============================] - 3s 321us/step\n", | |
"Puntaje: 1.14655178051\n", | |
"Presicion 0.5979\n" | |
] | |
} | |
], | |
"source": [ | |
"#Evaluando\n", | |
"score = model.evaluate(X_test,Y_test,\n", | |
" batch_size=BATCH_SIZE,verbose=VERBOSE)\n", | |
"print(\"Puntaje: \",score[0])\n", | |
"print('Presicion',score[1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"#Guardado de modelo\n", | |
"model.save_weights(\"cifar10_pesos.h5\",overwrite=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"conv2d_2 (Conv2D) (None, 32, 32, 32) 896 \n", | |
"_________________________________________________________________\n", | |
"activation_3 (Activation) (None, 32, 32, 32) 0 \n", | |
"_________________________________________________________________\n", | |
"conv2d_3 (Conv2D) (None, 32, 32, 32) 9248 \n", | |
"_________________________________________________________________\n", | |
"activation_4 (Activation) (None, 32, 32, 32) 0 \n", | |
"_________________________________________________________________\n", | |
"max_pooling2d_2 (MaxPooling2 (None, 16, 16, 32) 0 \n", | |
"_________________________________________________________________\n", | |
"dropout_2 (Dropout) (None, 16, 16, 32) 0 \n", | |
"_________________________________________________________________\n", | |
"conv2d_4 (Conv2D) (None, 16, 16, 64) 18496 \n", | |
"_________________________________________________________________\n", | |
"activation_5 (Activation) (None, 16, 16, 64) 0 \n", | |
"_________________________________________________________________\n", | |
"conv2d_5 (Conv2D) (None, 14, 14, 64) 36928 \n", | |
"_________________________________________________________________\n", | |
"activation_6 (Activation) (None, 14, 14, 64) 0 \n", | |
"_________________________________________________________________\n", | |
"max_pooling2d_3 (MaxPooling2 (None, 7, 7, 64) 0 \n", | |
"_________________________________________________________________\n", | |
"dropout_3 (Dropout) (None, 7, 7, 64) 0 \n", | |
"_________________________________________________________________\n", | |
"flatten_2 (Flatten) (None, 3136) 0 \n", | |
"_________________________________________________________________\n", | |
"dense_2 (Dense) (None, 512) 1606144 \n", | |
"_________________________________________________________________\n", | |
"dropout_4 (Dropout) (None, 512) 0 \n", | |
"_________________________________________________________________\n", | |
"dense_3 (Dense) (None, 10) 5130 \n", | |
"_________________________________________________________________\n", | |
"activation_7 (Activation) (None, 10) 0 \n", | |
"=================================================================\n", | |
"Total params: 1,676,842\n", | |
"Trainable params: 1,676,842\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"#Modelo 2 mejorado\n", | |
"model2 = Sequential()\n", | |
"model2.add(Conv2D(32,kernel_size=3,padding='same',\n", | |
" input_shape=(IMG_ROWS,IMG_COLS,IMG_CHANELS)))\n", | |
"model2.add(Activation('relu'))\n", | |
"model2.add(Conv2D(32,kernel_size=3,padding='same'))\n", | |
"model2.add(Activation('relu'))\n", | |
"model2.add(MaxPooling2D(pool_size=(2,2)))\n", | |
"model2.add(Dropout(.25))\n", | |
"model2.add(Conv2D(64,kernel_size=3,padding='same'))\n", | |
"model2.add(Activation('relu'))\n", | |
"model2.add(Conv2D(64,kernel_size=3))\n", | |
"model2.add(Activation('relu'))\n", | |
"model2.add(MaxPooling2D(pool_size=(2,2)))\n", | |
"model2.add(Dropout(.25))\n", | |
"model2.add(Flatten())\n", | |
"model2.add(Dense(512))\n", | |
"model2.add(Dropout(.5))\n", | |
"model2.add(Dense(NB_CLASSES))\n", | |
"model2.add(Activation('softmax'))\n", | |
"model2.summary()\n", | |
"#Entrenar\n", | |
"model2.compile(loss='categorical_crossentropy',\n", | |
" optimizer=OPIMIZER,\n", | |
" metrics=['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Train on 40000 samples, validate on 10000 samples\n", | |
"Epoch 1/20\n", | |
"40000/40000 [==============================] - 304s 8ms/step - loss: 1.6460 - acc: 0.4048 - val_loss: 1.2532 - val_acc: 0.5528\n", | |
"Epoch 2/20\n", | |
"40000/40000 [==============================] - 307s 8ms/step - loss: 1.1826 - acc: 0.5829 - val_loss: 0.9569 - val_acc: 0.6681\n", | |
"Epoch 3/20\n", | |
"40000/40000 [==============================] - 308s 8ms/step - loss: 1.0073 - acc: 0.6502 - val_loss: 1.0642 - val_acc: 0.6326\n", | |
"Epoch 4/20\n", | |
"40000/40000 [==============================] - 303s 8ms/step - loss: 0.9107 - acc: 0.6830 - val_loss: 0.7950 - val_acc: 0.7166\n", | |
"Epoch 5/20\n", | |
"40000/40000 [==============================] - 298s 7ms/step - loss: 0.8442 - acc: 0.7063 - val_loss: 0.7784 - val_acc: 0.7213\n", | |
"Epoch 6/20\n", | |
"40000/40000 [==============================] - 303s 8ms/step - loss: 0.7967 - acc: 0.7242 - val_loss: 0.7338 - val_acc: 0.7451\n", | |
"Epoch 7/20\n", | |
"40000/40000 [==============================] - 315s 8ms/step - loss: 0.7647 - acc: 0.7328 - val_loss: 0.7674 - val_acc: 0.7343\n", | |
"Epoch 8/20\n", | |
"40000/40000 [==============================] - 312s 8ms/step - loss: 0.7390 - acc: 0.7440 - val_loss: 0.7710 - val_acc: 0.7352\n", | |
"Epoch 9/20\n", | |
"40000/40000 [==============================] - 316s 8ms/step - loss: 0.7103 - acc: 0.7530 - val_loss: 0.8495 - val_acc: 0.7170\n", | |
"Epoch 10/20\n", | |
"40000/40000 [==============================] - 310s 8ms/step - loss: 0.6906 - acc: 0.7598 - val_loss: 0.7262 - val_acc: 0.7538\n", | |
"Epoch 11/20\n", | |
"40000/40000 [==============================] - 308s 8ms/step - loss: 0.6642 - acc: 0.7689 - val_loss: 0.7318 - val_acc: 0.7527\n", | |
"Epoch 12/20\n", | |
"40000/40000 [==============================] - 306s 8ms/step - loss: 0.6532 - acc: 0.7751 - val_loss: 0.7413 - val_acc: 0.7478\n", | |
"Epoch 13/20\n", | |
"40000/40000 [==============================] - 310s 8ms/step - loss: 0.6425 - acc: 0.7772 - val_loss: 0.6976 - val_acc: 0.7621\n", | |
"Epoch 14/20\n", | |
"40000/40000 [==============================] - 310s 8ms/step - loss: 0.6311 - acc: 0.7813 - val_loss: 0.7028 - val_acc: 0.7649\n", | |
"Epoch 15/20\n", | |
"40000/40000 [==============================] - 310s 8ms/step - loss: 0.6109 - acc: 0.7874 - val_loss: 0.7126 - val_acc: 0.7628\n", | |
"Epoch 16/20\n", | |
"40000/40000 [==============================] - 310s 8ms/step - loss: 0.6061 - acc: 0.7907 - val_loss: 0.6751 - val_acc: 0.7751\n", | |
"Epoch 17/20\n", | |
"40000/40000 [==============================] - 305s 8ms/step - loss: 0.5889 - acc: 0.7962 - val_loss: 0.7055 - val_acc: 0.7604\n", | |
"Epoch 18/20\n", | |
"40000/40000 [==============================] - 301s 8ms/step - loss: 0.5830 - acc: 0.7972 - val_loss: 0.7047 - val_acc: 0.7707\n", | |
"Epoch 19/20\n", | |
"40000/40000 [==============================] - 301s 8ms/step - loss: 0.5762 - acc: 0.7982 - val_loss: 0.6651 - val_acc: 0.7773\n", | |
"Epoch 20/20\n", | |
"40000/40000 [==============================] - 299s 7ms/step - loss: 0.5630 - acc: 0.8051 - val_loss: 0.7770 - val_acc: 0.7527\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f9875fb2c18>" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\n", | |
"\n", | |
"model2.fit(X_train,Y_train,batch_size=64,\n", | |
" epochs=NB_EPOCHS,\n", | |
" validation_split=VALIDATION_SPLIT,\n", | |
" verbose = VERBOSE)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model2.save_weights('cifar 10 pesos modelo 2.h5',overwrite=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import scipy.misc\n", | |
"from keras.models import model_from_json\n", | |
"from keras.optimizers import SGD" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[3]\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/home/harpie/anaconda/envs/keras/lib/python3.5/site-packages/ipykernel_launcher.py:2: DeprecationWarning: `imread` is deprecated!\n", | |
"`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", | |
"Use ``imageio.imread`` instead.\n", | |
" \n", | |
"/home/harpie/anaconda/envs/keras/lib/python3.5/site-packages/ipykernel_launcher.py:2: DeprecationWarning: `imresize` is deprecated!\n", | |
"`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", | |
"Use ``skimage.transform.resize`` instead.\n", | |
" \n" | |
] | |
} | |
], | |
"source": [ | |
"img_names = [\"imagen/gato.jpg\"]\n", | |
"imgs = [np.transpose(scipy.misc.imresize(scipy.misc.imread(x),(32,32)),(1,0,2)).astype(\"float32\") for x in img_names]\n", | |
"imgs = np.array(imgs) / 255\n", | |
"# predicciones\n", | |
"pred = model2.predict_classes(imgs)\n", | |
"print(pred)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://www.cs.toronto.edu/~kriz/cifar.html \n", | |
"\n", | |
"# segun cifar-10 tenemos 10 categorias evaluadas del 0 al 9 \n", | |
"# 1. Aeroplanos\n", | |
"# 2. Automoviles\n", | |
"# 3. Pajaros\n", | |
"# 4. Gatos\n", | |
"# 5. Benados\n", | |
"# 6. Perros\n", | |
"# 7. Ranas \n", | |
"# 8. Caballos\n", | |
"# 9. Obejas\n", | |
"# 10. Camiones\n", | |
"\n", | |
"# Si seguimos la logica de empezar por el 0 el 3 serian gatos y por la imagen mostrada es correcta la prediccion" | |
] | |
} | |
], | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment