text_plain_output_1.png
import cv2

import numpy as np

import pickle

import tensorflow as tf

IMG_SIZE = 512

import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
import os

import pickle
pickle_in_train_x = open('../input/preprocessed-data-aptos2019blindnessdetection/train_x_aptos2019-blindness-detection.pickle', 'rb')
pickle_in_train_y = open('../input/preprocessed-data-aptos2019blindnessdetection/train_y_aptos2019-blindness-detection.pickle', 'rb')
pickle_in_test_x = open('../input/preprocessed-data-aptos2019blindnessdetection/test_x_aptos2019-blindness-detection.pickle', 'rb')
train_x = pickle.load(pickle_in_train_x)
train_y = pickle.load(pickle_in_train_y)
test_x = pickle.load(pickle_in_test_x)

"""
def crop_image1(img,tol=7):
    # img is image data
    # tol  is tolerance
    mask = img>tol
    return img[np.ix_(mask.any(1),mask.any(0))]
"""
def crop_image_from_gray(img, tol=7):
    if img.ndim == 2:
        mask = img > tol
        return img[np.ix_(mask.any(1), mask.any(0))]
    elif img.ndim == 3:
        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        mask = gray_img > tol
        check_shape = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))].shape[0]
        if check_shape == 0:
            return img
        else:
            img1 = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))]
            img2 = img[:, :, 1][np.ix_(mask.any(1), mask.any(0))]
            img3 = img[:, :, 2][np.ix_(mask.any(1), mask.any(0))]
            img = np.stack([img1, img2, img3], axis=-1)
        return img

def load_ben_color(data, img_size, sigmaX=10):
    if data.ndim == 4:
        for i in range(len(data)):
            image = cv2.cvtColor(data[i], cv2.COLOR_BGR2RGB)
            image = crop_image_from_gray(image)
            image = cv2.resize(image, (img_size, img_size))
            data[i] = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0, 0), sigmaX), -4, 128)
    elif data.ndim == 3:
        data = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
        data = crop_image_from_gray(data)
        data = cv2.resize(data, (img_size, img_size))
        data = cv2.addWeighted(data, 4, cv2.GaussianBlur(data, (0, 0), sigmaX), -4, 128)
    else:
        return 0
    return data

train_x = load_ben_color(train_x, IMG_SIZE, sigmaX=10)

train_x = train_x.astype('float32')

def create_model_1():
    layers_1 = [tf.keras.layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation=tf.nn.relu, input_shape=train_x.shape[1:]), tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2)), tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation=tf.nn.relu), tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2)), tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation=tf.nn.relu), tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2)), tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation=tf.nn.relu), tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2)), tf.keras.layers.Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation=tf.nn.relu), tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(units=512, activation=tf.nn.relu), tf.keras.layers.Dense(units=256, activation=tf.nn.relu), tf.keras.layers.Dense(units=len(np.unique(train_y)), activation=tf.nn.softmax)]
    model_1 = tf.keras.Sequential(layers_1)
    model_1.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
    return model_1

model_1 = create_model_1()
model_1.summary()