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

from keras.preprocessing.image import ImageDataGenerator

import math

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 tensorflow as tf

import tensorflow_addons as tfa

import tensorflow_addons as tfa

train_img_csv = '../input/my-pre-data/MURA-v1.1/abdekho_train.csv'
train_images_paths = pd.read_csv(os.path.join(train_img_csv), dtype=str, header=None)
train_images_paths.columns = ['image_path']
valid_img_csv = '../input/my-pre-data/MURA-v1.1/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']
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_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_FINGER = train_images_paths[train_images_paths['category'] == 'XR_FINGER']
valid_images_paths_XR_FINGER = valid_images_paths[valid_images_paths['category'] == 'XR_FINGER']

images_path_dir = '../input/my-pre-data'
datagen = ImageDataGenerator(samplewise_center=True, samplewise_std_normalization=True, rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.1, horizontal_flip=True, vertical_flip=True)
valid_datagen = ImageDataGenerator(rescale=1.0 / 255, rotation_range=30)
targetsize = (224, 224)
classmode = 'binary'
batchsize = 32
train_generator = 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, shuffle=True)
valid_generator = valid_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, shuffle=True)

input_image = Input(shape=(224, 224, 3), name='original_img')
dense_model_1 = tf.keras.applications.DenseNet169(include_top=False, weights='imagenet')
x = dense_model_1(input_image, training=True)
x = tf.keras.layers.Conv2D(20, (1, 1), activation='relu')(x)
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.Dropout(0.3)(x)
x = tf.keras.layers.Dense(42, activation='relu')(x)
x = tf.keras.layers.Dropout(0.3)(x)
preds_dense_net = tf.keras.layers.Dense(1, activation='sigmoid')(x)
dense_model_2 = tf.keras.applications.ResNet50(weights='imagenet', include_top=False)
y = dense_model_2(input_image, training=True)
y = tf.keras.layers.Conv2D(20, (1, 1), activation='relu')(y)
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.Dropout(0.3)(y)
y = tf.keras.layers.Dense(42, activation='relu')(y)
y = tf.keras.layers.Dropout(0.3)(y)
preds_resnet_net = tf.keras.layers.Dense(1, activation='sigmoid')(y)
dense_model_3 = tf.keras.applications.MobileNet(include_top=False, weights='imagenet')
z = dense_model_3(input_image, training=True)
z = tf.keras.layers.Conv2D(20, (1, 1), activation='relu')(z)
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.Dropout(0.3)(z)
z = tf.keras.layers.Dense(42, activation='relu')(z)
z = tf.keras.layers.Dropout(0.3)(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)
ensemble_model = tf.keras.models.Model(input_image, mean_nn_only)
ensemble_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=[tfa.metrics.CohenKappa(num_classes=2), 'accuracy'])

STEP_SIZE_TRAIN = math.ceil(train_generator.n / train_generator.batch_size)
STEP_SIZE_VALID = math.ceil(valid_generator.n / valid_generator.batch_size)
ensemble_model.fit_generator(generator=train_generator, steps_per_epoch=STEP_SIZE_TRAIN, validation_data=valid_generator, validation_steps=STEP_SIZE_VALID, epochs=15, verbose=1)
_, cohen, acc = ensemble_model.evaluate(train_generator, verbose=1)
print(' accuracy overall: %.3f' % acc, end=' ')
print('kappa overall: %.3f' % cohen)