text_plain_output_1.png
from keras.models import Sequential,Model,load_model,Input

from keras_preprocessing.image import ImageDataGenerator

import os

import os

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import tensorflow as tf

import pandas as pd
import os
from glob import glob
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
from keras.models import Model
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
from glob import glob
import numpy as np
from keras import regularizers
from keras.models import Sequential, Model, load_model, Input
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, GlobalAveragePooling2D
from keras_preprocessing.image import ImageDataGenerator
import keras.layers as Layers
from keras.callbacks import EarlyStopping, ModelCheckpoint
import keras.optimizers as Optimizer
from keras import applications
from tensorflow import keras
import math
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import tensorflow_addons as tfa

train_img_csv = '../input/mura-dataset/MURA-v1.1/train_image_paths.csv'
train_images_paths = pd.read_csv(os.path.join(train_img_csv), dtype=str, header=None)
train_images_paths.columns = ['image_path']
train_images_paths['label'] = train_images_paths['image_path'].map(lambda x: '1' if 'positive' in x else '0')
train_images_paths['category'] = train_images_paths['image_path'].apply(lambda x: x.split('/')[2])
valid_img_csv = '../input/testdata/abdekho_valid.csv'
valid_images_paths = pd.read_csv(os.path.join(valid_img_csv), dtype=str, header=None)
valid_images_paths.columns = ['image_path']
valid_images_paths['label'] = valid_images_paths['image_path'].map(lambda x: '1' if 'positive' in x else '0')
valid_images_paths['category'] = valid_images_paths['image_path'].apply(lambda x: x.split('/')[2])

train_images_paths_XR_ELBOW = train_images_paths[train_images_paths['category'] == 'XR_ELBOW']
valid_images_paths_XR_ELBOW = valid_images_paths[valid_images_paths['category'] == 'XR_ELBOW']
train_images_paths_XR_FINGER = train_images_paths[train_images_paths['category'] == 'XR_FINGER']
valid_images_paths_XR_FINGER = valid_images_paths[valid_images_paths['category'] == 'XR_FINGER']
train_images_paths_XR_FOREARM = train_images_paths[train_images_paths['category'] == 'XR_FOREARM']
valid_images_paths_XR_FOREARM = valid_images_paths[valid_images_paths['category'] == 'XR_FOREARM']
train_images_paths_XR_HAND = train_images_paths[train_images_paths['category'] == 'XR_HAND']
valid_images_paths_XR_HAND = valid_images_paths[valid_images_paths['category'] == 'XR_HAND']
train_images_paths_XR_HUMERUS = train_images_paths[train_images_paths['category'] == 'XR_HUMERUS']
valid_images_paths_XR_HUMERUS = valid_images_paths[valid_images_paths['category'] == 'XR_HUMERUS']
train_images_paths_XR_SHOULDER = train_images_paths[train_images_paths['category'] == 'XR_SHOULDER']
valid_images_paths_XR_SHOULDER = valid_images_paths[valid_images_paths['category'] == 'XR_SHOULDER']
train_images_paths_XR_WRIST = train_images_paths[train_images_paths['category'] == 'XR_WRIST']
valid_images_paths_XR_WRIST = valid_images_paths[valid_images_paths['category'] == 'XR_WRIST']

datagen = ImageDataGenerator(rescale=1.0 / 255, rotation_range=10, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1.0 / 255, rotation_range=10)
images_path_dir = '../input/mura-dataset'
batchsize = 32
targetsize = (224, 224)
classmode = 'binary'
train_generator = datagen.flow_from_dataframe(dataframe=train_images_paths, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator = test_datagen.flow_from_dataframe(dataframe=valid_images_paths, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
train_generator_XR_ELBOW = datagen.flow_from_dataframe(dataframe=train_images_paths_XR_ELBOW, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_ELBOW = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_ELBOW, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
train_generator_XR_FINGER = datagen.flow_from_dataframe(dataframe=train_images_paths_XR_FINGER, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_FINGER = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_FINGER, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
train_generator_XR_FOREARM = datagen.flow_from_dataframe(dataframe=train_images_paths_XR_FOREARM, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_FOREARM = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_FOREARM, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
train_generator_XR_HAND = datagen.flow_from_dataframe(dataframe=train_images_paths_XR_HAND, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_HAND = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_HAND, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
train_generator_XR_HUMERUS = datagen.flow_from_dataframe(dataframe=train_images_paths_XR_HUMERUS, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_HUMERUS = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_HUMERUS, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
train_generator_XR_SHOULDER = datagen.flow_from_dataframe(dataframe=train_images_paths_XR_SHOULDER, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_SHOULDER = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_SHOULDER, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
train_generator_XR_WRIST = datagen.flow_from_dataframe(dataframe=train_images_paths_XR_WRIST, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_WRIST = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_WRIST, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)

input_image = Input(shape=(224, 224, 3), name='original_img')
dense_model_1 = tf.keras.applications.DenseNet169(include_top=False, weights='imagenet')
dense_model_1.trainable = True
for layer in dense_model_1.layers[:350]:
    layer.trainable = False
x = dense_model_1(input_image)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(81, activation='relu')(x)
x = tf.keras.layers.Dense(81, activation='relu')(x)
x = tf.keras.layers.Dense(42, activation='relu')(x)
preds_dense_net = tf.keras.layers.Dense(1, activation='sigmoid')(x)
dense_model_2 = tf.keras.applications.Xception(weights='imagenet', include_top=False)
dense_model_2.trainable = True
for layer in dense_model_2.layers[:116]:
    layer.trainable = False
y = dense_model_2(input_image)
y = tf.keras.layers.GlobalAveragePooling2D()(y)
y = tf.keras.layers.Dense(81, activation='relu')(y)
y = tf.keras.layers.Dense(81, activation='relu')(y)
y = tf.keras.layers.Dense(42, activation='relu')(y)
preds_resnet_net = tf.keras.layers.Dense(1, activation='sigmoid')(y)
dense_model_3 = tf.keras.applications.MobileNet(include_top=False, weights='imagenet')
dense_model_3.trainable = True
for layer in dense_model_3.layers[:70]:
    layer.trainable = False
z = dense_model_3(input_image)
z = tf.keras.layers.GlobalAveragePooling2D()(z)
z = tf.keras.layers.Dense(81, activation='relu')(z)
z = tf.keras.layers.Dense(81, activation='relu')(z)
z = tf.keras.layers.Dense(42, activation='relu')(z)
preds_mobi_net = tf.keras.layers.Dense(1, activation='sigmoid')(z)
mean_nn_only = tf.reduce_mean(tf.stack([preds_mobi_net, preds_resnet_net, preds_dense_net], axis=0), axis=0)
model = tf.keras.models.Model(input_image, mean_nn_only)

valid_img_csv = '../input/testdata/abdekho_valid.csv'
valid_images_paths = pd.read_csv(os.path.join(valid_img_csv), dtype=str, header=None)
valid_images_paths.columns = ['image_path']
valid_images_paths['label'] = valid_images_paths['image_path'].map(lambda x: '1' if 'positive' in x else '0')
valid_images_paths['category'] = valid_images_paths['image_path'].apply(lambda x: x.split('/')[2])

valid_images_paths_XR_ELBOW = valid_images_paths[valid_images_paths['category'] == 'XR_ELBOW']
valid_images_paths_XR_FINGER = valid_images_paths[valid_images_paths['category'] == 'XR_FINGER']
valid_images_paths_XR_FOREARM = valid_images_paths[valid_images_paths['category'] == 'XR_FOREARM']
valid_images_paths_XR_HAND = valid_images_paths[valid_images_paths['category'] == 'XR_HAND']
valid_images_paths_XR_HUMERUS = valid_images_paths[valid_images_paths['category'] == 'XR_HUMERUS']
valid_images_paths_XR_SHOULDER = valid_images_paths[valid_images_paths['category'] == 'XR_SHOULDER']
valid_images_paths_XR_WRIST = valid_images_paths[valid_images_paths['category'] == 'XR_WRIST']

test_datagen = ImageDataGenerator(rescale=1.0 / 255)
images_path_dir = '../input/mura-dataset'
batchsize = 16
targetsize = (224, 224)
classmode = 'binary'
valid_generator = test_datagen.flow_from_dataframe(dataframe=valid_images_paths, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_ELBOW = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_ELBOW, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_FINGER = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_FINGER, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_FOREARM = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_FOREARM, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_HAND = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_HAND, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_HUMERUS = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_HUMERUS, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_SHOULDER = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_SHOULDER, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)
valid_generator_XR_WRIST = test_datagen.flow_from_dataframe(dataframe=valid_images_paths_XR_WRIST, directory=images_path_dir, x_col='image_path', y_col='label', target_size=targetsize, class_mode=classmode, batch_size=batchsize, suffle=True)