# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/muhdlaziem/DR/blob/master/Training_1x2.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="8BW2rOQfd9kx" colab_type="code" outputId="2361460c-2a44-4b95-c83b-ccd71093600e" colab={"base_uri": "https://localhost:8080/", "height": 52}
from google.colab import drive
drive.mount('/gdrive')
# %cd /gdrive/'My Drive'/

# + id="UxzrGOcsZTJ9" colab_type="code" colab={}
import numpy as np

# + id="cp0lzQpfZTKW" colab_type="code" colab={}
trainImages = np.load('./diabetic-retinopathy-resized/1x2/trainImages.npy')
testImages = np.load('./diabetic-retinopathy-resized/1x2/testImages.npy')
y_train = np.load('./diabetic-retinopathy-resized/1x2/y_train.npy')
y_test = np.load('./diabetic-retinopathy-resized/1x2/y_test.npy')

# + id="ux_GwMX8ZTKi" colab_type="code" outputId="d5cf1b28-3bab-4695-c7aa-a23766cfa9e8" colab={"base_uri": "https://localhost:8080/", "height": 86}
print(trainImages.shape)
print(testImages.shape)
print(y_train.shape)
print(y_test.shape)

# + id="0zvMC0sHZTKu" colab_type="code" outputId="b2587e2c-c1f7-41b3-aac7-db24ebf243bc" colab={"base_uri": "https://localhost:8080/", "height": 52}
print(trainImages.max())
print(testImages.max())

# + id="h5myAgrycc5-" colab_type="code" colab={}
# Normalize
trainImages = trainImages/255
testImages = testImages/255

# + id="FdlhksgnZTK5" colab_type="code" outputId="d601c955-1ccc-4bd5-f8fc-29534782c090" colab={"base_uri": "https://localhost:8080/", "height": 64}
from tensorflow.keras import datasets, layers, models


# + id="-BTf6EJwZTLC" colab_type="code" colab={}
def convolutional_model():
    
    # create model
    model = models.Sequential()
    model.add(layers.Conv2D(128, (5, 5), activation='relu', input_shape=(256, 256, 3)))
    model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    # model.add(layers.Dropout(0.3))

    model.add(layers.Conv2D(64, (5,5), strides = (1,1), activation='relu'))
    model.add(layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)))
    # model.add(layers.Dropout(0.5))

    model.add(layers.Conv2D(32, (5,5), strides = (1,1), activation='relu'))
    model.add(layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)))
    # model.add(layers.Dropout(0.3))
    
    model.add(layers.Flatten())
    model.add(layers.Dense(100, activation='relu'))
    # model.add(layers.Dropout(0.3))
    model.add(layers.Dense(2, activation='softmax'))

    # model = models.Sequential()
    # model.add(layers.Conv2D(16, (5, 5), activation='relu', input_shape=(256, 256, 3)))
    # model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    
    # model.add(layers.Conv2D(8, (2, 2), activation='relu'))
    # model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    
    # model.add(layers.Flatten())
    # model.add(layers.Dense(100, activation='relu'))
    # model.add(layers.Dense(2, activation='softmax'))
    
    model.summary()
    model.compile(optimizer='adam', loss='categorical_crossentropy',  metrics=['accuracy'])
    
    return model


# + id="9Wq_OaajZTLK" colab_type="code" outputId="435074d5-74b2-4ace-db04-15181f57c7b9" colab={"base_uri": "https://localhost:8080/", "height": 541}
model = convolutional_model()

# + id="D1ii7PQAZTLQ" colab_type="code" outputId="1e789d0f-f04f-42fd-aff7-5f2ef8a7dccf" colab={"base_uri": "https://localhost:8080/", "height": 1000}
model.fit(trainImages, y_train, epochs=40, batch_size=100, validation_data=(testImages, y_test))

# + id="lkPTmCSkdOEX" colab_type="code" outputId="560b2827-7a3e-4568-f7eb-ed961f60763f" colab={"base_uri": "https://localhost:8080/", "height": 52}
scores = model.evaluate(testImages, y_test, verbose=0)
print('Accuracy: {}% \n Error: {}'.format(scores[1], 1 - scores[1])) 

# + id="x7GkOIoueMWf" colab_type="code" outputId="547940aa-ec13-49e6-d51c-ba3847beb393" colab={"base_uri": "https://localhost:8080/", "height": 52}
from sklearn import metrics
y_pred = model.predict(testImages)
matrix = metrics.confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
matrix

# + id="8HRPUBPgdVJH" colab_type="code" outputId="6c7beab6-27b1-45e9-8a33-b6396fa854cc" colab={"base_uri": "https://localhost:8080/", "height": 34}
model.save("training_1x2_1.h5")
print("Saved model to disk")

# + id="GB931bhIuDns" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered