#!/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, 'numpy.load', 'np.load', (['"""train_data.npy"""'], {}), True, 'import numpy as np\n'), (23, 'numpy.zeros', 'np.zeros', (['(210, 204, 204, 3)'], {}), True, 'import numpy as np\n'), (24, 'numpy.zeros', 'np.zeros', (['(210)'], {}), True, 'import numpy as np\n'), (34, 'sklearn.model_selection.train_test_split', 'train_test_split', (['x_data', 'y_data'], {'test_size': '(0.2)', 'random_state': '(42)'}), False, 'from sklearn.model_selection import train_test_split\n'), (45, 'tensorflow.keras.applications.VGG16', 'VGG16', ([], {'include_top': '(False)', 'weights': '"""vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"""', 'input_shape': '(204, 204, 3)'}), False, 'from tensorflow.keras.applications import VGG16\n'), (49, 'tensorflow.keras.Input', 'tf.keras.Input', ([], {'shape': '(204, 204, 3)'}), True, 'import tensorflow as tf\n'), (55, 'tensorflow.keras.Model', 'keras.Model', (['inputs', 'outputs'], {}), False, 'from tensorflow import keras\n'), (28, 'skimage.transform.resize', 'resize', (['img', '(204, 204)'], {}), False, 'from skimage.transform import resize\n'), (51, 'tensorflow.keras.layers.Flatten', 'tf.keras.layers.Flatten', ([], {}), True, 'import tensorflow as tf\n'), (52, 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (['(256)'], {'activation': '"""relu"""'}), True, 'import tensorflow as tf\n'), (53, 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (['(64)'], {'activation': '"""relu"""'}), True, 'import tensorflow as tf\n'), (54, 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (['(2)'], {'activation': '"""softmax"""'}), True, 'import tensorflow as tf\n'), (61, 'tensorflow.keras.optimizers.SGD', 'tf.keras.optimizers.SGD', ([], {'learning_rate': '(0.001)'}), True, 'import tensorflow as tf\n'), (39, 'sklearn.preprocessing.OneHotEncoder', 'OneHotEncoder', ([], {}), False, 'from sklearn.preprocessing import OneHotEncoder\n'), (40, 'sklearn.preprocessing.OneHotEncoder', 'OneHotEncoder', ([], {}), False, 'from sklearn.preprocessing import OneHotEncoder\n')]