In [1]:
%matplotlib inline
In [2]:
from __future__ import division,print_function
import shutil
import os, json
from glob import glob
import numpy as np
np.set_printoptions(precision=4, linewidth=100)
from matplotlib import pyplot as plt
In [3]:
%ls
In [5]:
%mkdir ~/nbs/hw1/data
%cd ~/nbs/hw1/data
%mkdir valid sample
%mkdir sample/valid sample/train
In [7]:
%cd ~/nbs/hw1/data/train
imgs = glob('*.jpg')
rand = np.random.permutation(imgs)
for i in range(1999): os.rename(rand[i], '../valid/'+rand[i])
%ls -l ../valid | wc -l
In [8]:
from shutil import copyfile
In [9]:
img = glob('*.jpg')
img_rand = np.random.permutation(img)
for i in range(49): copyfile(img_rand[i], '../sample/valid/'+img_rand[i])
%ls -l ../sample/valid | wc -l
In [10]:
img = glob('*.jpg')
img_rand = np.random.permutation(img)
for i in range(199): copyfile(img_rand[i], '../sample/train/'+img_rand[i])
%ls -l ../sample/train | wc -l
In [11]:
%cd ~/nbs/hw1/data/train
%mkdir cats dogs
%mv cat.*.jpg cats/
%mv dog.*.jpg dogs/
In [12]:
%cd ~/nbs/hw1/data/valid
%mkdir cats dogs
%mv cat.*.jpg cats/
%mv dog.*.jpg dogs/
In [13]:
%cd ~/nbs/hw1/data/sample/train
%mkdir cats dogs
%mv cat.*.jpg cats/
%mv dog.*.jpg dogs/
In [14]:
%cd ~/nbs/hw1/data/sample/valid
%mkdir cats dogs
%mv cat.*.jpg cats/
%mv dog.*.jpg dogs/
In [15]:
%cd ~/nbs/hw1
In [16]:
path = "data/"
#path = "data/sample/"
In [17]:
import utils; reload(utils)
from utils import plots
In [ ]:
# As large as you can, but no larger than 64 is recommended.
# If you have an older or cheaper GPU, you'll run out of memory, so will have to decrease this.
batch_size=64
In [ ]:
# Import our class, and instantiate
from vgg16 import Vgg16
In [70]:
vgg = Vgg16()
In [71]:
batches = vgg.get_batches(path+'train', batch_size=batch_size)
val_batches = vgg.get_batches(path+'valid', batch_size=batch_size*2)
In [72]:
vgg.finetune(batches)
In [ ]:
vgg.fit(batches, val_batches, nb_epoch=1)
In [30]:
vgg.model.save_weights(path+'results/ft1.h5')
In [8]:
ft1 = vgg.model.load_weights(path+'results/ft1.h5')
In [ ]:
batches = vgg.test(path+'test', batch_size=batch_size*2)
In [ ]:
batches
In [ ]:
predictions = vgg.model.predict_generator(batches, batches.nb_sample)
In [ ]: