<jupyter_start><jupyter_text>Load data<jupyter_code>from sklearn.datasets import load_files       
from keras.utils import np_utils
import numpy as np
from glob import glob

def download_dataset(url, path='./'):
    print("  Downloading dataset from", url)
    return wget.download(url)

def unzip(filename, path='./'):
    with open(filename, 'rb') as f:
        z = zipfile.ZipFile(f)
        print("  Unzipping file", filename)
        for name in z.namelist():
            print("    Extracting file", name)
            z.extract(name,path)
        
def load_dataset(path):
    data = load_files(path)
    dog_files = np.array(data['filenames'])
    dog_targets = np_utils.to_categorical(np.array(data['target']), 133)
    return dog_files, dog_targets

#only the first time
#url = 'https://s3-us-west-1.amazonaws.com/udacity-aind/dog-project/dogImages.zip'
#filename = download_dataset(url, path='./')
#unzip(filename, path='./')

train_files, train_targets = load_dataset('dogImages/train')
valid_files, valid_targets = load_dataset('dogImages/valid')
test_files, test_targets = load_dataset('dogImages/test')

dog_names = [item[20:-1] for item in sorted(glob("dogImages/train/*/"))]

# Let's check the dataset
print('There are %d total dog categories.' % len(dog_names))
print('There are %s total dog images.\n' % len(np.hstack([train_files, valid_files, test_files])))
print('There are %d training dog images.' % len(train_files))
print('There are %d validation dog images.' % len(valid_files))
print('There are %d test dog images.'% len(test_files))
train_files<jupyter_output><empty_output><jupyter_text>Pre-process data<jupyter_code>from keras.preprocessing import image                  
from tqdm import tqdm

def path_to_tensor(img_path):
    # loads RGB image as PIL.Image.Image type
    img = image.load_img(img_path, target_size=(224, 224))
    # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
    x = image.img_to_array(img)
    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
    return np.expand_dims(x, axis=0)

def paths_to_tensor(img_paths):
    list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
    return np.vstack(list_of_tensors)
from PIL import ImageFile                            
ImageFile.LOAD_TRUNCATED_IMAGES = True                 

# pre-process the data for Keras
train_tensors = paths_to_tensor(train_files).astype('float32')/255
valid_tensors = paths_to_tensor(valid_files).astype('float32')/255
test_tensors = paths_to_tensor(test_files).astype('float32')/255<jupyter_output>100%|██████████| 6680/6680 [00:43<00:00, 154.68it/s]
100%|██████████| 835/835 [01:33<00:00,  8.94it/s]
100%|██████████| 836/836 [00:11<00:00, 75.57it/s]<jupyter_text>Fine-tune a pre-trained model (transfer learning)<jupyter_code>from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input as preprocess_input_resnet50

def extract_Resnet50(file_paths):
    tensors = paths_to_tensor(file_paths).astype('float32')
    preprocessed_input = preprocess_input_resnet50(tensors)
    return ResNet50(weights='imagenet', include_top=False).predict(preprocessed_input, batch_size=32)<jupyter_output><empty_output><jupyter_text>Extract feature<jupyter_code>train_resnet50 = extract_Resnet50(train_files)
valid_resnet50 = extract_Resnet50(valid_files)
test_resnet50 = extract_Resnet50(test_files)
print("Resnet50 shape", train_resnet50.shape[1:])<jupyter_output>100%|██████████| 6680/6680 [01:35<00:00, 70.05it/s]
100%|██████████| 835/835 [00:15<00:00, 55.07it/s]
100%|██████████| 836/836 [00:13<00:00, 61.45it/s]<jupyter_text>Retrain the last layers for our data<jupyter_code>from keras.layers.pooling import GlobalAveragePooling2D
from keras.layers.merge import Concatenate
from keras.layers import Input, Dense
from keras.layers.core import Dropout, Activation
from keras.callbacks import ModelCheckpoint
from keras.layers.normalization import BatchNormalization
from keras.models import Model

def input_branch(input_shape=None):
    
    size = int(input_shape[2] / 4)
    
    branch_input = Input(shape=input_shape)
    branch = GlobalAveragePooling2D()(branch_input)
    branch = Dense(size, use_bias=False, kernel_initializer='uniform')(branch)
    branch = BatchNormalization()(branch)
    branch = Activation("relu")(branch)
    return branch, branch_input

resnet50_branch, resnet50_input = input_branch(input_shape=(1, 1, 2048))
net = Dropout(0.3)(resnet50_branch)
net = Dense(640, use_bias=False, kernel_initializer='uniform')(net)
net = BatchNormalization()(net)
net = Activation("relu")(net)
net = Dropout(0.3)(net)
net = Dense(133, kernel_initializer='uniform', activation="softmax")(net)

model = Model(inputs=[resnet50_input], outputs=[net])
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         (None, 1, 1, 2048)        0         
_________________________________________________________________
global_average_pooling2d_2 ( (None, 2048)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 512)               1048576   
_________________________________________________________________
batch_normalization_2 (Batch (None, 512)               2048      
_________________________________________________________________
activation_149 (Activation)  (None, 512)               0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (D[...]<jupyter_text>Compile and fit the network<jupyter_code>model.compile(loss='categorical_crossentropy', optimizer="rmsprop", metrics=['accuracy'])
checkpointer = ModelCheckpoint(filepath='saved_models/bestmodel.hdf5', 
                               verbose=1, save_best_only=True)
model.fit([train_resnet50], train_targets, 
          validation_data=([valid_resnet50], valid_targets),
          epochs=10, batch_size=4, callbacks=[checkpointer], verbose=1)<jupyter_output>Train on 6680 samples, validate on 835 samples
Epoch 1/10
6680/6680 [==============================] - 32s 5ms/step - loss: 1.6179 - acc: 0.5540 - val_loss: 0.8094 - val_acc: 0.7521

Epoch 00001: val_loss improved from inf to 0.80943, saving model to saved_models/bestmodel.hdf5
Epoch 2/10
6680/6680 [==============================] - 30s 5ms/step - loss: 1.3993 - acc: 0.6124 - val_loss: 0.7210 - val_acc: 0.7653

Epoch 00002: val_loss improved from 0.80943 to 0.72104, saving model to saved_models/bestmodel.hdf5
Epoch 3/10
6680/6680 [==============================] - 30s 5ms/step - loss: 1.2926 - acc: 0.6446 - val_loss: 0.6950 - val_acc: 0.7749

Epoch 00003: val_loss improved from 0.72104 to 0.69498, saving model to saved_models/bestmodel.hdf5
Epoch 4/10
6680/6680 [==============================] - 31s 5ms/step - loss: 1.1550 - acc: 0.6692 - val_loss: 0.6824 - val_acc: 0.7796

Epoch 00004: val_loss improved from 0.69498 to 0.68243, saving model to saved_models/bestmodel.hdf5
Epoch 5/10
66[...]<jupyter_text>Test the model<jupyter_code>model.load_weights('saved_models/bestmodel.hdf5')

from sklearn.metrics import accuracy_score

predictions = model.predict([test_resnet50])
breed_predictions = [np.argmax(prediction) for prediction in predictions]
breed_true_labels = [np.argmax(true_label) for true_label in test_targets]
print('Test accuracy: %.4f%%' % (accuracy_score(breed_true_labels, breed_predictions) * 100))<jupyter_output>Test accuracy: 83.2536%
{'fraction_non_alphanumeric': 0.13864572605561276, 'fraction_numerical': 0.06771369721936148, 'mean_word_length': 4.935064935064935, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '328160', 'license': 'MIT', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2526, 'n_words': 538, 'path': 'Ship type classification - resnet.ipynb', 'repo_name': 'innovationgarage/02ML', 'size': '7768'}