Kaggle – Dogs vs. Cats Redux (tensorflow編)

今回は、このサイトのコードを参考にしながらkaggleのdogs vs. cats reduxをやる。先ず最初にkaggleのサイトから今回のチュートリアルに必要な画像データを入手してくる。

ダウンロードしたアーカイブファイルを解凍する。

!unzip all.zip
Archive:  all.zip
  inflating: train.zip               
  inflating: sample_submission.csv   
  inflating: test.zip                

dataサブディレクトリを作成する。

!mkdir data

解凍したアーカイブファイルをdataフォルダに移動する。

!mv train.zip data
!mv test.zip data

dataディレクトリに移動する。

cd data
/home/workspace/download/data

train.zipとtest.zipを解凍する。

!unzip train.zip
!unzip test.zip

必要なモジュールとデータのインポート

import cv2                 # working with, mainly resizing, images
import numpy as np         # dealing with arrays
import os                  # dealing with directories
from random import shuffle # mixing up or currently ordered data that might lead our network astray in training.
from tqdm import tqdm      # a nice pretty percentage bar for tasks.
TRAIN_DIR = 'train'
TEST_DIR = 'test'
IMG_SIZE = 50
LR = 1e-3
MODEL_NAME = 'dogsvscats-{}-{}.model'.format(LR, '2conv-basic') # just so we remember which saved model is which, sizes must match
def label_img(img):
    word_label = img.split('.')[-3]
    # conversion to one-hot array [cat,dog]
    #                            [much cat, no dog]
    if word_label == 'cat': return [1,0]
    #                             [no cat, very doggo]
    elif word_label == 'dog': return [0,1]
def create_train_data():
    training_data = []
    for img in tqdm(os.listdir(TRAIN_DIR)):
        label = label_img(img)
        path = os.path.join(TRAIN_DIR,img)
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
        training_data.append([np.array(img),np.array(label)])
    shuffle(training_data)
    np.save('train_data.npy', training_data)
    return training_data
def process_test_data():
    testing_data = []
    for img in tqdm(os.listdir(TEST_DIR)):
        path = os.path.join(TEST_DIR,img)
        img_num = img.split('.')[0]
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
        testing_data.append([np.array(img), img_num])
        
    shuffle(testing_data)
    np.save('test_data.npy', testing_data)
    return testing_data
train_data = create_train_data()
# If you have already created the dataset:
#train_data = np.load('train_data.npy')
100%|██████████| 25000/25000 [00:13<00:00, 1897.69it/s]

tflearnのインストール

import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')

if os.path.exists('{}.meta'.format(MODEL_NAME)):
    model.load(MODEL_NAME)
    print('model loaded!')
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-20-a152c158c264> in <module>()
----> 1 import tflearn
      2 from tflearn.layers.conv import conv_2d, max_pool_2d
      3 from tflearn.layers.core import input_data, dropout, fully_connected
      4 from tflearn.layers.estimator import regression
      5 

ModuleNotFoundError: No module named 'tflearn'
!pip3 install tflearn
Collecting tflearn
  Downloading https://files.pythonhosted.org/packages/16/ec/e9ce1b52e71f6dff3bd944f020cef7140779e783ab27512ea7c7275ddee5/tflearn-0.3.2.tar.gz (98kB)
    100% |################################| 102kB 3.9MB/s a 0:00:011
Requirement already satisfied: numpy in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from tflearn) (1.14.4)
Requirement already satisfied: six in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from tflearn) (1.11.0)
Requirement already satisfied: Pillow in /root/.pyenv/versions/3.6.5/envs/py365/lib/python3.6/site-packages (from tflearn) (5.1.0)
Building wheels for collected packages: tflearn
  Running setup.py bdist_wheel for tflearn ... done
  Stored in directory: /root/.cache/pip/wheels/d0/f6/69/0ef3ee395aac2e5d15d89efd29a9a216f3c27767b43b72c006
Successfully built tflearn
Installing collected packages: tflearn
Successfully installed tflearn-0.3.2
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')

if os.path.exists('{}.meta'.format(MODEL_NAME)):
    model.load(MODEL_NAME)
    print('model loaded!')
WARNING:tensorflow:From /root/.pyenv/versions/py365/lib/python3.6/site-packages/tflearn/initializations.py:119: UniformUnitScaling.__init__ (from tensorflow.python.ops.init_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.initializers.variance_scaling instead with distribution=uniform to get equivalent behavior.
WARNING:tensorflow:From /root/.pyenv/versions/py365/lib/python3.6/site-packages/tflearn/objectives.py:66: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead

モデルのトレーニング

train_data = np.load('download/data/train_data.npy')
train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=3, validation_set=({'input': test_x}, {'targets': test_y}), 
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
Training Step: 1148  | total loss: 11.50826 | time: 3.656s
| Adam | epoch: 003 | loss: 11.50826 - acc: 0.5002 -- iter: 24448/24500
Training Step: 1149  | total loss: 11.54471 | time: 4.669s
| Adam | epoch: 003 | loss: 11.54471 - acc: 0.4986 | val_loss: 11.32872 - val_acc: 0.5080 -- iter: 24500/24500
--
import tensorflow as tf
tf.reset_default_graph()
#size matters - bigger network
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')

if os.path.exists('{}.meta'.format(MODEL_NAME)):
    model.load(MODEL_NAME)
    print('model loaded!')

train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'targets': test_y}), 
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

model.save(MODEL_NAME)
Training Step: 3829  | total loss: 0.35553 | time: 4.510s
| Adam | epoch: 010 | loss: 0.35553 - acc: 0.8432 -- iter: 24448/24500
Training Step: 3830  | total loss: 0.35025 | time: 5.525s
| Adam | epoch: 010 | loss: 0.35025 - acc: 0.8448 | val_loss: 0.51573 - val_acc: 0.7940 -- iter: 24500/24500
--
INFO:tensorflow:/home/workspace/dogsvscats-0.001-2conv-basic.model is not in all_model_checkpoint_paths. Manually adding it.
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = 20, 15
plt.rcParams["font.size"] = "16"

# if you need to create the data:
test_data = process_test_data()

# if you already have some saved:
#test_data = np.load('test_data.npy')

fig=plt.figure()

for num,data in enumerate(test_data[:12]):
    # cat: [1,0]
    # dog: [0,1]
    
    img_num = data[1]
    img_data = data[0]
    
    y = fig.add_subplot(3,4,num+1)
    orig = img_data
    data = img_data.reshape(IMG_SIZE,IMG_SIZE,1)
    #model_out = model.predict([data])[0]
    model_out = model.predict([data])[0]
    
    if np.argmax(model_out) == 1: str_label='Dog'
    else: str_label='Cat'
        
    y.imshow(orig,cmap='gray')
    plt.title(str_label)
    y.axes.get_xaxis().set_visible(False)
    y.axes.get_yaxis().set_visible(False)
plt.show()
100%|██████████| 12500/12500 [00:05<00:00, 2119.28it/s]

検証精度が0.7940なので、間違いが結構ある。

csvファイルの作成

with open('submission_file.csv','w') as f:
    f.write('id,label\n')
with open('submission_file.csv','a') as f:
    for data in tqdm(test_data):
        img_num = data[1]
        img_data = data[0]
        orig = img_data
        data = img_data.reshape(IMG_SIZE,IMG_SIZE,1)
        model_out = model.predict([data])[0]
        f.write('{},{}\n'.format(img_num,model_out[1]))
100%|██████████| 12500/12500 [00:24<00:00, 513.66it/s]
!head -n 11 submission_file.csv
id,label
3372,0.995048463344574
2304,0.7259718179702759
101,0.8440787196159363
4999,0.9337702393531799
11377,0.1705828160047531
9036,0.43236541748046875
6179,0.24908895790576935
11709,0.973800778388977
1874,0.9946263432502747
12147,0.6994239091873169
import pandas as pd

pd.read_csv("submission_file.csv", nrows=11)
id label
0 3372 0.995048
1 2304 0.725972
2 101 0.844079
3 4999 0.933770
4 11377 0.170583
5 9036 0.432365
6 6179 0.249089
7 11709 0.973801
8 1874 0.994626
9 12147 0.699424
10 3206 0.974514