import json
import math
import os
import cv2
from PIL import Image
import numpy as np
from keras import layers
from keras.applications import DenseNet201
from keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import cohen_kappa_score, accuracy_score
import scipy
from tqdm import tqdm
import tensorflow as tf
from keras import backend as K
import gc
from functools import partial
from sklearn import metrics
from collections import Counter
import json
import itertools
import json
import math
import os
import cv2
from PIL import Image
import numpy as np
from keras import layers
from keras.applications import DenseNet201
from keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import cohen_kappa_score, accuracy_score
import scipy
from tqdm import tqdm
import tensorflow as tf
from keras import backend as K
import gc
from functools import partial
from sklearn import metrics
from collections import Counter
import json
import itertools
##图像加载defDataset_loader(DIR, RESIZE, sigmaX=10):
IMG =[]
read =lambda imname: np.asarray(Image.open(imname).convert("RGB"))for IMAGE_NAME in tqdm(os.listdir(DIR)):
PATH = os.path.join(DIR,IMAGE_NAME)
_, ftype = os.path.splitext(PATH)if ftype ==".png":
img = read(PATH)
img = cv2.resize(img,(RESIZE,RESIZE))
IMG.append(np.array(img))return IMG
#创建数组
benign_train = np.array(Dataset_loader('C:/Users/11711/Desktop/data/train/benign',224))
malign_train = np.array(Dataset_loader('C:/Users/11711/Desktop/data/train/malignant',224))
benign_test = np.array(Dataset_loader('C:/Users/11711/Desktop/data/validation/benign',224))
malign_test = np.array(Dataset_loader('C:/Users/11711/Desktop/data/validation/malignant',224))
benign_train_label = np.zeros(len(benign_train))
malign_train_label = np.ones(len(malign_train))
benign_test_label = np.zeros(len(benign_test))
malign_test_label = np.ones(len(malign_test))
X_train = np.concatenate((benign_train, malign_train), axis =0)
Y_train = np.concatenate((benign_train_label, malign_train_label), axis =0)
X_test = np.concatenate((benign_test, malign_test), axis =0)
Y_test = np.concatenate((benign_test_label, malign_test_label), axis =0)
s = np.arange(X_train.shape[0])
np.random.shuffle(s)
X_train = X_train[s]
Y_train = Y_train[s]
s = np.arange(X_test.shape[0])
np.random.shuffle(s)
X_test = X_test[s]
Y_test = Y_test[s]
Y_train = to_categorical(Y_train, num_classes=2)
Y_test = to_categorical(Y_test, num_classes=2)#分组
x_train, x_val, y_train, y_val = train_test_split(
X_train, Y_train,
test_size=0.2,
random_state=11)
w=60
h=40
fig=plt.figure(figsize=(15,15))
columns =4
rows =3for i inrange(1, columns*rows +1):
ax = fig.add_subplot(rows, columns, i)if np.argmax(Y_train[i])==0:
ax.title.set_text('Benign')else:
ax.title.set_text('Malignant')
plt.imshow(x_train[i], interpolation='nearest')
plt.show()#创建数据生成器
BATCH_SIZE =16
train_generator = ImageDataGenerator(
zoom_range=2,# 设置范围为随机缩放
rotation_range =90,
horizontal_flip=True,# 随机翻转图片
vertical_flip=True,# 随机翻转图片)#构建模型defbuild_model(backbone, lr=1e-4):
model = Sequential()
model.add(backbone)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dropout(0.5))
model.add(layers.BatchNormalization())
model.add(layers.Dense(2, activation='softmax'))
model.compile(
loss='binary_crossentropy',
optimizer=Adam(lr=lr),
metrics=['accuracy'])return model
resnet = DenseNet201(
weights='imagenet',
include_top=False,
input_shape=(224,224,3))
model = build_model(resnet ,lr =1e-4)
model.summary()#定义回调函数&训练模型
learn_control = ReduceLROnPlateau(monitor='val_acc', patience=5,
verbose=1,factor=0.2, min_lr=1e-7)
filepath="weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
history = model.fit_generator(
train_generator.flow(x_train, y_train, batch_size=BATCH_SIZE),
steps_per_epoch=x_train.shape[0]/ BATCH_SIZE,
epochs=20,
validation_data=(x_val, y_val),
callbacks=[learn_control, checkpoint])
history_df = pd.DataFrame(history.history)
history_df[['loss','val_loss']].plot()
history_df = pd.DataFrame(history.history)
history_df[['acc','val_acc']].plot()from sklearn.metrics import classification_report
classification_report( np.argmax(Y_test, axis=1), np.argmax(Y_pred_tta, axis=1))from sklearn.metrics import confusion_matrix
defplot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):if normalize:
cm = cm.astype('float')/ cm.sum(axis=1)[:, np.newaxis]print("Normalized confusion matrix")else:print('Confusion matrix, without normalization')print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=55)
plt.yticks(tick_marks, classes)
fmt ='.2f'if normalize else'd'
thresh = cm.max()/2.for i, j in itertools.product(range(cm.shape[0]),range(cm.shape[1])):
plt.text(j, i,format(cm[i, j], fmt),
horizontalalignment="center",
color="white"if cm[i, j]> thresh else"black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
cm = confusion_matrix(np.argmax(Y_test, axis=1), np.argmax(Y_pred, axis=1))
cm_plot_label =['benign','malignant']
plot_confusion_matrix(cm, cm_plot_label, title ='Confusion Metrix for Skin Cancer')