<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Hands-on DL course
Instalation
From base anaconda
Step1:  Preprocessing
Step2:  FC network
keras notation
Step3:  CNN
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import dogs_vs_cats as dvc
import matplotlib.pyplot as plt
%matplotlib inline
all_files = dvc.image_files()
n_images_train=5000
n_images_val=500
n_images_test=500
input_image_shape = (50,50,3) 
train_val_features, train_val_labels,train_val_files, \
test_features, test_labels, test_files = dvc.training_test_datasets(all_files,
                                                                    n_images_train+n_images_val,n_images_test,
                                                                    input_image_shape)
# split train and val
index_files_selected = np.random.permutation(n_images_train+n_images_val)
train_files = train_val_files[index_files_selected[:n_images_train]]
val_files = train_val_files[index_files_selected[n_images_train:]]
train_features = train_val_features[index_files_selected[:n_images_train]]
val_features = train_val_features[index_files_selected[n_images_train:]]
train_labels = train_val_labels[index_files_selected[:n_images_train]]
val_labels = train_val_labels[index_files_selected[n_images_train:]]
train_features.shape, train_labels.shape,train_files[:10],train_labels[:10]
index_example = 31
fig,ax = plt.subplots(1,2,figsize=(14,8))
ax[0].imshow(train_features[index_example,]/255)
ax[1].set_title(train_files[index_example])
_ = ax[1].hist(train_features[index_example,].ravel(),bins=40)
from IPython.display import Image,display
display(Image(train_files[index_example]))
Explanation: Hands-on DL course
Instalation
From base anaconda:
pip install keras
This will install keras and theano.
In windows we have to configure theano use the windows compiler:
conda install mingw libpython
Data exploration
End of explanation
#media = np.mean(train_features,axis=(0,2,3),keepdims=True)
#print(media.shape,media.ravel())
#media = media[:,np.newaxis,np.newaxis]
#train_features-=media
#val_features-=media
#test_features-=media
from keras.applications.imagenet_utils import preprocess_input
preprocesado = False
if not preprocesado:
    train_features = preprocess_input(train_features)
    val_features = preprocess_input(val_features)
    test_features = preprocess_input(test_features)
    preprocesado = True
Explanation: Preprocessing
End of explanation
input_shape_flat = np.prod(input_image_shape)
train_features_flat = train_features.reshape((train_features.shape[0],input_shape_flat))
val_features_flat = val_features.reshape((val_features.shape[0],input_shape_flat))
test_features_flat = test_features.reshape((test_features.shape[0],input_shape_flat))
print(train_features_flat.shape,val_features_flat.shape,test_features_flat.shape)
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation
train_fc_model = True
if train_fc_model:
    fc_model = Sequential([
        Dense(1024, input_dim=input_shape_flat),
        Activation('sigmoid'),
        Dense(512),
        Activation('sigmoid'),
        Dense(256),
        Activation('sigmoid'),
        Dense(1),
        Activation('sigmoid')
    ])
    fc_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    nb_epoch=20
    hist=fc_model.fit(train_features_flat,
                      train_labels, 
                      epochs=nb_epoch,validation_data=(val_features_flat,
                                                   val_labels),
                      batch_size=32,verbose=2)
    fc_model.save("fc_model.h5")
else:
    fc_model = keras.models.load_model("fc_model_trained.h5")
    
fc_model.summary()
if train_fc_model:
    hist.history
if train_fc_model:
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.title('FCNN Loss Trend')
    plt.plot(hist.history["loss"], 'blue', label='Training Loss')
    plt.plot(hist.history["val_loss"], 'green', label='Validation Loss')
    #plt.xticks(range(0,nb_epoch,2))
    plt.legend()
if train_fc_model:
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.title('FCNN Accuracy Trend')
    plt.plot(hist.history["acc"], 'blue', label='Training Accuracy')
    plt.plot(hist.history["val_acc"], 'green', label='Validation Accuracy')
    plt.legend()
results = fc_model.evaluate(test_features_flat,test_labels)
print("")
print(" ".join(["%s: %.4f"%(metric_name,valor) for metric_name,valor in zip(fc_model.metrics_names,results)]))
preds = fc_model.predict(test_features_flat)
dvc.plotROC(test_labels,preds)
Explanation: FC network
keras notation:
* epoch:  Each epoch is a full loop over all the training data.
* nb_epoch: Number of epochs to train the model. 
* batch_size: Number of samples to use in each stochastic gradient update.
For example, 80 epochs consist on 80 loops over all the training examples. If there are 1000 examples and batch size is set to 32 there will be 1000/32 * 80 gradient updates. Each gradient is estimated using 32 samples.
End of explanation
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import Adam
from keras.layers.normalization import BatchNormalization
from keras.callbacks import EarlyStopping
train_cnn_model = True
def catdog_cnn(input_image_shape):
    model = Sequential()
    model.add(Conv2D(32, (3, 3),input_shape=input_image_shape))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(32, (3, 3)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    
    model.add(Conv2D(64, (3, 3)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(64, 3, 3))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    
    model.add(Conv2D(128, (3, 3)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(128, (3, 3)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    
    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    
    model.add(Activation('sigmoid'))
    optimizer = Adam()
    objective = 'binary_crossentropy'
    model.compile(loss=objective, optimizer=optimizer, metrics=['accuracy'])
    return model
if train_cnn_model:
    cnn_model = catdog_cnn(input_image_shape)
else:
    cnn_model = keras.models.load_model("cnn_model_trained.h5")
cnn_model.summary()
if train_cnn_model:
    nb_epoch=80
    print("Model compiled, start training")
    early_stopping_callback = EarlyStopping(monitor='val_loss', min_delta=0, patience=8, 
                                            verbose=1, mode='auto')
    history = cnn_model.fit(train_features, train_labels,validation_data=(val_features,val_labels),
                            batch_size=32, epochs=nb_epoch,verbose=2,callbacks=[early_stopping_callback])
    cnn_model.save("cnn_model.h5")
if train_cnn_model:
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.title('CNN Loss Trend')
    plt.plot(history.history["loss"], 'blue', label='Training Loss')
    plt.plot(history.history["val_loss"], 'green', label='Validation Loss')
    #plt.xticks(range(0,nb_epoch,2))
    plt.legend(loc="best")
if train_cnn_model:
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.title('CNN Accuracy Trend')
    plt.plot(history.history["acc"], 'blue', label='Training Accuracy')
    plt.plot(history.history["val_acc"], 'green', label='Validation Accuracy')
    plt.legend(loc="best")
# evaluate the model
results = cnn_model.evaluate(test_features,test_labels)
print("")
print(" ".join(["%s: %.4f"%(metric_name,valor) for metric_name,valor in zip(cnn_model.metrics_names,results)]))
preds = cnn_model.predict(test_features)
dvc.plotROC(test_labels,preds)
Explanation: CNN
End of explanation
<END_TASK>