application_vnd.jupyter.stderr_output_1.png
from keras.layers.convolutional import Convolution2D, MaxPooling2D

from keras.layers.core import Dense, Dropout, Activation, Flatten

from keras.models import Sequential

from keras.models import model_from_json

from keras.optimizers import SGD

import cv2

import cv2

import glob

import glob

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import os

import os

import pickle

import random

import random
labels = [1, 2, 3]
count = 0
for l in labels:
    train_files = ['../input/train/Type_' + str(l) + '/' + f for f in os.listdir('../input/train/Type_' + str(l) + '/')]
    random_file = random.choice(train_files)
    im = cv2.imread(random_file)
    plt.axis('off')
    count += 1

img_rows = 224
img_cols = 224
def get_im_cv2(path, img_rows, img_cols, color_type=3):
    if color_type == 1:
        img = cv2.imread(path, 0)
    elif color_type == 3:
        img = cv2.imread(path)
    resized = cv2.resize(img, (img_cols, img_rows))
    return resized
def load_train(img_rows, img_cols, color_type=3):
    X_train = []
    y_train = []
    for j in range(1, 4):
        path = os.path.join('..', 'input', 'train', 'Type_' + str(j), '*.jpg')
        files = glob.glob(path)
        for fl in files:
            flbase = os.path.basename(fl)
            img = get_im_cv2(fl, img_rows, img_cols, color_type)
            X_train.append(img)
            y_train.append(j)
    return (X_train, y_train)
X_train, y_train = load_train(64, 64, 3)

def cache_data(data, path):
    if os.path.isdir(os.path.dirname(path)):
        file = open(path, 'wb')
        pickle.dump(data, file)
        file.close()
def restore_data(path):
    data = dict()
    if os.path.isfile(path):
        file = open(path, 'rb')
        data = pickle.load(file)
    return data

def save_model(model):
    json_string = model.to_json()
    if not os.path.isdir('cache'):
        os.mkdir('cache')
    open(os.path.join('cache', 'architecture.json'), 'w').write(json_string)
    model.save_weights(os.path.join('cache', 'model_weights.h5'), overwrite=True)
def read_model():
    model = model_from_json(open(os.path.join('cache', 'architecture.json')).read())
    model.load_weights(os.path.join('cache', 'model_weights.h5'))
    return model

def create_model(img_rows, img_cols, color_type=3):
    nb_classes = 3
    nb_filters = 8
    nb_pool = 2
    nb_conv = 2
    model = Sequential()
    model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(color_type, img_rows, img_cols)))
    model.add(Activation('relu'))
    model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))
    sgd = SGD(lr=0.01, decay=0, momentum=0, nesterov=False)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)
    return model

nb_epoch = 3
batch_size = 16
model = create_model_v1(img_rows, img_cols, color_type_global)
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1)