#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 18 22:42:54 2020

@author: mike
"""


import numpy as np
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
from tensorflow.keras.applications import VGG16
from tensorflow.keras import layers
from sklearn.preprocessing import OneHotEncoder
from skimage.transform import resize
import matplotlib.pyplot as plt


train_data = np.load("train_data.npy")

x_data = np.zeros((210,204,204,3))
y_data = np.zeros(210)

for i in range(210):
    img = train_data[i,1:].reshape(1024,1024)
    img_resized = resize(img,(204,204))
    y_data[i] = train_data[i,0]
    x_data[i,:,:,0] = img_resized.astype(int)
    x_data[i,:,:,1] = img_resized.astype(int)
    x_data[i,:,:,2] = img_resized.astype(int)

x_train, x_test, y_train, y_test = train_test_split(
        x_data, y_data, test_size=0.2, random_state=42)



y_train = OneHotEncoder().fit_transform(y_train.reshape(-1,1)).toarray()
y_test = OneHotEncoder().fit_transform(y_test.reshape(-1,1)).toarray()
    



base_model = VGG16(include_top=False, weights='vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', 
                   input_shape=(204, 204, 3))
base_model.trainable = False

inputs = tf.keras.Input(shape=(204, 204, 3))
x = base_model(inputs)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(256, activation='relu')(x)
x = tf.keras.layers.Dense(64, activation='relu')(x)
outputs = tf.keras.layers.Dense(2, activation='softmax')(x)
model = keras.Model(inputs, outputs)

model.summary()



model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),loss="binary_crossentropy",metrics=["accuracy"])


model.fit(x_train, y_train, batch_size=16, epochs=5)


pred = model.predict(x_train)
score = model.evaluate(x_test, y_test, verbose=0)
print(score[0],score[1])
[((21, 13, 21, 38), 'numpy.load', 'np.load', ({(21, 21, 21, 37): '"""train_data.npy"""'}, {}), "('train_data.npy')", True, 'import numpy as np\n'), ((23, 9, 23, 34), 'numpy.zeros', 'np.zeros', ({(23, 18, 23, 33): '(210, 204, 204, 3)'}, {}), '((210, 204, 204, 3))', True, 'import numpy as np\n'), ((24, 9, 24, 22), 'numpy.zeros', 'np.zeros', ({(24, 18, 24, 21): '210'}, {}), '(210)', True, 'import numpy as np\n'), ((34, 35, 35, 55), 'sklearn.model_selection.train_test_split', 'train_test_split', (), '', False, 'from sklearn.model_selection import train_test_split\n'), ((45, 13, 46, 45), 'tensorflow.keras.applications.VGG16', 'VGG16', (), '', False, 'from tensorflow.keras.applications import VGG16\n'), ((49, 9, 49, 44), 'tensorflow.keras.Input', 'tf.keras.Input', (), '', True, 'import tensorflow as tf\n'), ((55, 8, 55, 36), 'tensorflow.keras.Model', 'keras.Model', ({(55, 20, 55, 26): 'inputs', (55, 28, 55, 35): 'outputs'}, {}), '(inputs, outputs)', False, 'from tensorflow import keras\n'), ((28, 18, 28, 39), 'skimage.transform.resize', 'resize', ({(28, 25, 28, 28): 'img', (28, 29, 28, 38): '(204, 204)'}, {}), '(img, (204, 204))', False, 'from skimage.transform import resize\n'), ((51, 4, 51, 29), 'tensorflow.keras.layers.Flatten', 'tf.keras.layers.Flatten', ({}, {}), '()', True, 'import tensorflow as tf\n'), ((52, 4, 52, 49), 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (), '', True, 'import tensorflow as tf\n'), ((53, 4, 53, 48), 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (), '', True, 'import tensorflow as tf\n'), ((54, 10, 54, 56), 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (), '', True, 'import tensorflow as tf\n'), ((61, 24, 61, 68), 'tensorflow.keras.optimizers.SGD', 'tf.keras.optimizers.SGD', (), '', True, 'import tensorflow as tf\n'), ((39, 10, 39, 25), 'sklearn.preprocessing.OneHotEncoder', 'OneHotEncoder', ({}, {}), '()', False, 'from sklearn.preprocessing import OneHotEncoder\n'), ((40, 9, 40, 24), 'sklearn.preprocessing.OneHotEncoder', 'OneHotEncoder', ({}, {}), '()', False, 'from sklearn.preprocessing import OneHotEncoder\n')]