text_plain_output_1.png
from keras import layers

from keras import models

from keras.applications import VGG16

from keras.preprocessing import image

from keras.preprocessing.image import ImageDataGenerator

import numpy as np # linear algebra

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import numpy as np
import pandas as pd
import os
train_path = '../input/train/train'
test_path = '../input/test/test'
label_frame = pd.read_csv('../input/train.csv')
test_frame = pd.read_csv('../input/sample_submission.csv')
x_train = []
x_test = []
y_train = np.array(label_frame['has_cactus'])
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
for fname in label_frame['id']:
    image_path = os.path.join(train_path, fname)
    pil_image = image.load_img(image_path, target_size=(32, 32, 3))
    np_image = image.img_to_array(pil_image)
    x_train.append(np_image)
for fname in test_frame['id']:
    image_path = os.path.join(test_path, fname)
    pil_image = image.load_img(image_path, target_size=(32, 32, 3))
    np_image = image.img_to_array(pil_image)
    x_test.append(np_image)
x_train = np.array(x_train)
x_train = x_train.astype('float32') / 255
x_test = np.array(x_test)
x_test = x_test.astype('float32') / 255

augmentations = ImageDataGenerator(vertical_flip=True, horizontal_flip=True, zoom_range=0.1)
augmentations.fit(x_train)

from keras.applications import VGG16
from keras import models
from keras import layers
from keras import optimizers
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
def get_model():
    base = VGG16(include_top=False, weights='imagenet', input_shape=(32, 32, 3))
    base.trainable = True
    base.summary()
    set_trainable = False
    for layer in base.layers:
        if layer.name == 'block5_conv3':
            set_trainable = True
        if set_trainable:
            layer.trainable = True
        else:
            layer.trainable = False
    model = models.Sequential()
    model.add(base)
    model.add(layers.Flatten())
    model.add(layers.BatchNormalization())
    model.add(layers.Dense(256, activation='relu'))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(1, activation='sigmoid'))
    model.summary()
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
    return model
model = get_model()
"\nx_val = x_train[16600:]\ny_val = y_train[16600:]\n\nmodel_check_point = ModelCheckpoint('./model.h5',monitor = 'val_loss',save_best_only = True)\nearly_stopping = EarlyStopping(monitor = 'val_loss',patience = 25)\nreduce_lr_on_plateau = ReduceLROnPlateau(monitor = 'val_loss',patience = 15)\n\nhistory = model.fit(x_train[:16600],y_train[:16600],epochs = 80,batch_size = 250,validation_data = (x_val,y_val),        callbacks = [model_check_point,reduce_lr_on_plateau])\n#visualize\nimport matplotlib.pyplot as plt\n\nacc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()\n"

model.fit_generator(augmentations.flow(x_train, y_train), epochs=100, steps_per_epoch=250)
y_predictions = model.predict(x_test)
result = pd.DataFrame({'id': pd.read_csv('../input/sample_submission.csv')['id'], 'has_cactus': y_predictions.squeeze()})
result.to_csv('submissionMax.csv', index=False, columns=['id', 'has_cactus'])
print('submit successful')