Kaggle competition: 2019 SMHRD 경진대회 ( 지능형 )
<SOS> metric: categorizationaccuracy Kaggle data source: smhrd12<load_from_csv>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 train_df_aug = pd.read_csv('.. /input/moredata/external/External/Label.csv')
 print(train_df.shape)
 print(test_df.shape)
 print(train_df_aug.shape)
 train_df.head()
train_df_aug.head()<train_model>
train = pd.read_csv('.. /input/smhrd12/train.csv')
 test = pd.read_csv('.. /input/smhrd12/test.csv' )
def get_pad_width(im, new_shape, is_rgb=True):
pad_diff = new_shape - im.shape[0], new_shape - im.shape[1]
t, b = math.floor(pad_diff[0]/2), math.ceil(pad_diff[0]/2)
 l, r = math.floor(pad_diff[1]/2), math.ceil(pad_diff[1]/2)
 if is_rgb:
pad_width =(( t,b),(l,r),(0, 0))
else:
pad_width =(( t,b),(l,r))
return pad_width
def preprocess_image(image_path, desired_size=224):
im = Image.open(image_path)
 im = im.resize(( desired_size,)*2, resample=Image.LANCZOS)
 return im<prepare_x_and_y>
train['sex']=pd.get_dummies(train['sex'],prefix='Sex')
 test['sex']=pd.get_dummies(test['sex'],prefix='Sex')
 encoder = LabelEncoder()
train['workclass'] = encoder.fit_transform(train['workclass'])
 test['workclass'] = encoder.transform(test['workclass'])
 encoder = LabelEncoder()
train['education'] = encoder.fit_transform(train['education'])
 test['education'] = encoder.transform(test['education'])
 encoder = LabelEncoder()
train['marital-status'] = encoder.fit_transform(train['marital-status'])
 test['marital-status'] = encoder.transform(test['marital-status'])
 encoder = LabelEncoder()
train['occupation'] = encoder.fit_transform(train['occupation'])
 test['occupation'] = encoder.transform(test['occupation'])
 encoder = LabelEncoder()
train['relationship'] = encoder.fit_transform(train['relationship'])
 test['relationship'] = encoder.transform(test['relationship'])
 encoder = LabelEncoder()
train['race'] = encoder.fit_transform(train['race'])
 test['race'] = encoder.transform(test['race'])
 encoder = LabelEncoder()
train['native-country'] = encoder.fit_transform(train['native-country'])
 test['native-country'] = encoder.transform(test['native-country'])
IMAGE_SIZE = 224
def info_image(im):
cy = im.shape[0]//2
midline = im[cy,:]
midline = np.where(midline>midline.mean() /3)[0]
if len(midline)>im.shape[1]//2:
x_start, x_end = np.min(midline), np.max(midline)
 else:
x_start, x_end = im.shape[1]//10, 9*im.shape[1]//10
cx =(x_start + x_end)/2
r =(x_end - x_start)/2
return cx, cy, r
def resize_image(im, augmentation=True):
cx, cy, r = info_image(im)
 scaling = IMAGE_SIZE/(2*r)
 rotation = 0
if augmentation:
scaling *= 1 + 0.3 *(np.random.rand() -0.5)
 rotation = 360 * np.random.rand()
M = cv2.getRotationMatrix2D(( cx,cy), rotation, scaling)
 M[0,2] -= cx - IMAGE_SIZE/2
M[1,2] -= cy - IMAGE_SIZE/2
return cv2.warpAffine(im,M,(IMAGE_SIZE,IMAGE_SIZE))<categorify>
train['rel+gain'] = train['relationship']+train['capital-gain']
train['loss-gain'] = train['capital-loss']-train['capital-gain']
train['rel+gain+edu'] = train['relationship']+train['capital-gain']+train['education-num']
train['rel+gain+edu+age'] = train['relationship']+train['capital-gain']+train['education-num']
train['(rel+gain+edu+age)*rel'] =(train['relationship']+train['capital-gain']+train['education-num']+train['capital-loss'])*train['relationship']
def toCLAHEgreen(img):
clipLimit=2.0
tileGridSize=(8, 8)
 img = np.array(img)
 green_channel = img[:, :, 1]
clahe = cv2.createCLAHE(clipLimit=clipLimit, tileGridSize=tileGridSize)
 cla = clahe.apply(green_channel)
 cla=clahe.apply(cla)
 return cla<categorify>
y_train = pd.get_dummies(new_train_df['diagnosis'] ).values
print(x_train.shape)
 print(y_train.shape)
 print(x_test.shape )<categorify>
del train['no']
df2 = train
Y = df2['income']
del df2['income']
X = df2.iloc[:,:]
y_train_multi = np.empty(y_train.shape, dtype=y_train.dtype)
 y_train_multi[:, 4] = y_train[:, 4]
for i in range(3, -1, -1):
y_train_multi[:, i] = np.logical_or(y_train[:, i], y_train_multi[:, i+1])
 print("Original y_train:", y_train.sum(axis=0))
print("Multilabel version:", y_train_multi.sum(axis=0))<split>
del test['no']
test['rel+gain'] = test['relationship']+test['capital-gain']
test['loss-gain'] = test['capital-loss']-test['capital-gain']
test['rel+gain+edu'] = test['relationship']+test['capital-gain']+test['education-num']
test['rel+gain+edu+age'] = test['relationship']+test['capital-gain']+test['education-num']
test['(rel+gain+edu+age)*rel'] =(test['relationship']+test['capital-gain']+test['education-num']+test['capital-loss'])*test['relationship']
x_train, x_val, y_train, y_val = train_test_split(
 x_train, y_train_multi,
test_size=0.15,
random_state=2019
)<prepare_x_and_y>
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.2 , random_state=40)
 xtree = ExtraTreesClassifier(n_estimators=100, random_state=100)
 xtree.fit(X_train, Y_train)
 y_pred = xtree.predict(X_test)
 xtree_score = cross_val_score(xtree,X_train,Y_train,cv=5 ).mean()
print('Accuracy =', accuracy_score(Y_test, y_pred))
print("I : {}, 교차 검증 점수 : {:.3f}".format(0,xtree_score))
print("I : {}, 훈련 세트 정확도: {:.3f}".format(0,xtree.score(X_train, Y_train)))
 print("I : {}, 테스트 세트 정확도: {:.3f}".format(0,xtree.score(X_test, Y_test)))
 xtree_scoreT = cross_val_score(xtree,X_test,Y_test,cv=5 ).mean()
print("테스트 교차검증" ,xtree_scoreT )
BATCH_SIZE = 32
x_train =
data_generator = ImageDataGenerator().flow(x_train, y_train, batch_size=BATCH_SIZE )<compute_train_metric>
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.2 , random_state=40)
 rf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=16, n_jobs=-1)
 rf.fit(X_train, Y_train)
 y_pred = rf.predict(X_test)
 rf_score = cross_val_score(rf,X_train,Y_train,cv=5 ).mean()
print('Accuracy =', accuracy_score(Y_test, y_pred))
print("I : {}, 교차 검증 점수 : {:.3f}".format(0,rf_score))
print("I : {}, 훈련 세트 정확도: {:.3f}".format(0,rf.score(X_train, Y_train)))
 print("I : {}, 테스트 세트 정확도: {:.3f}".format(0,rf.score(X_test, Y_test)))
 rf_scoreT = cross_val_score(rf,X_test,Y_test,cv=5 ).mean()
print("테스트 교차검증" ,rf_scoreT )
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_kappas = []
def on_epoch_end(self, epoch, logs={}):
X_val, y_val = self.validation_data[:2]
y_val = y_val.sum(axis=1)- 1
y_pred = self.model.predict(X_val)> 0.5
y_pred = y_pred.astype(int ).sum(axis=1)- 1
_val_kappa = cohen_kappa_score(
 y_val,
y_pred,
weights='quadratic' 
)
 self.val_kappas.append(_val_kappa)
 print(f"val_kappa: {_val_kappa:.4f}")
 if _val_kappa == max(self.val_kappas):
print("Validation Kappa has improved.Saving model.")
 self.model.save('model.h5')
 return<choose_model_class>
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.2 , random_state=40)
 rf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=16, n_jobs=-1)
 xtree = ExtraTreesClassifier(n_estimators=100, random_state=0)
 voting = VotingClassifier(
 estimators=[('rf', rf),('xtree', xtree)],
voting='hard')
 voting.fit(X_train, Y_train)
 vo_score = cross_val_score(voting,X_train,Y_train,cv=7 ).mean()
print(",교차 검증 점수 : {:.3f}".format(vo_score))
print(",훈련 세트 정확도: {:.3f}".format(voting.score(X_train, Y_train)))
 print(",테스트 세트 정확도: {:.3f}".format(voting.score(X_test, Y_test)))
 print('================================================')
densenet = DenseNet121(
 weights='.. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5',
include_top=False,
input_shape=(224,224,3)
 )<choose_model_class>
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.2 , random_state=40)
 scaler = StandardScaler()
scaler.fit(X_train)
 scaler.fit(test)
 test_sc = scaler.transform(test)
 test_sc = pd.DataFrame(test_sc,columns=test.columns)
 X_train_sc = scaler.transform(X_train)
 X_test_sc = scaler.transform(X_test)
 X_train_sc = pd.DataFrame(X_train_sc,columns=X_train.columns)
 X_test_sc = pd.DataFrame(X_test_sc,columns=X_train.columns)
 gbrt = GradientBoostingClassifier(random_state=100,max_depth=5,learning_rate=0.12)
 gbrt.fit(X_train_sc, Y_train)
 y_pred = gbrt.predict(X_test_sc)
 gbrt_score = cross_val_score(gbrt,X_train_sc,Y_train,cv=5 ).mean()
print('Accuracy =', accuracy_score(Y_test, y_pred))
print("I : {}, 교차 검증 점수 : {:.3f}".format(0,gbrt_score))
print("I : {}, 훈련 세트 정확도: {:.3f}".format(0,gbrt.score(X_train_sc, Y_train)))
 print("I : {}, 테스트 세트 정확도: {:.3f}".format(0,gbrt.score(X_test_sc, Y_test)))
 gbrt_scoreT = cross_val_score(gbrt,X_test,Y_test,cv=5 ).mean()
print(gbrt_scoreT)
 gbrt_pred = gbrt.predict(test_sc)
def build_model() :
model = Sequential()
model.add(densenet)
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dropout(0.5))
model.add(layers.Dense(5, activation='sigmoid'))
model.compile(
 loss='binary_crossentropy',
optimizer=Adam(lr=0.00005),
metrics=['accuracy'] 
)
 return model<train_model>
fi = gbrt.feature_importances_
importances = pd.DataFrame(fi,index=X.columns)
 importances.sort_values(by=0,ascending=False )
kappa_metrics = Metrics()
history = model.fit_generator(
 data_generator,
steps_per_epoch=x_train.shape[0] / BATCH_SIZE,
epochs=15,
validation_data=(x_val, y_val),
callbacks=[kappa_metrics]
)<compute_train_metric>
for i in [150] :
ada = AdaBoostClassifier(
 DecisionTreeClassifier(max_depth=3), n_estimators=200,
algorithm="SAMME.R", learning_rate=0.22)
 ada.fit(X_train, Y_train)
 ada_score = cross_val_score(ada,X_train,Y_train,cv=10 ).mean()
print("i : {} ,교차 검증 점수 : {:.3f}".format(i,ada_score))
print("i : {} ,훈련 세트 정확도: {:.3f}".format(i,ada.score(X_train, Y_train)))
 print("i : {} ,테스트 세트 정확도: {:.3f}".format(i,ada.score(X_test, Y_test)))
 print('================================================')
model.load_weights('model.h5')
 y_val_pred = model.predict(x_val)
 def compute_score_inv(threshold):
y1 = y_val_pred > threshold
y1 = y1.astype(int ).sum(axis=1)- 1
y2 = y_val.sum(axis=1)- 1
score = cohen_kappa_score(y1, y2, weights='quadratic')
 return 1 - score
simplex = scipy.optimize.minimize(
 compute_score_inv, 0.5, method='nelder-mead' 
)
 best_threshold = simplex['x'][0]<save_to_csv>
xgb = XGBClassifier(objective ='reg:linear', colsample_bytree = 0.6, learning_rate = 0.35,
max_depth = 7, n_estimators = 13)
 xgb.fit(X_train, Y_train)
 xgb_score = cross_val_score(xgb,X_train,Y_train,cv=10 ).mean()
print("i : {} ,교차 검증 점수 : {:.3f}".format(0,xgb_score))
print("i : {} ,훈련 세트 정확도: {:.3f}".format(0,xgb.score(X_train, Y_train)))
 print("i : {} ,테스트 세트 정확도: {:.3f}".format(0,xgb.score(X_test, Y_test)))
 print('================================================' )
y_test = model.predict(x_test)> 0.5
y_test = y_test.astype(int ).sum(axis=1)- 1
test_df['diagnosis'] = y_test
test_df.to_csv('submission.csv',index=False )<import_modules>
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.2 , random_state=40)
 gbrt = GradientBoostingClassifier(random_state=101,max_depth=5,learning_rate=0.12)
 ada = AdaBoostClassifier(
 DecisionTreeClassifier(max_depth=3), n_estimators=200,
algorithm="SAMME.R", learning_rate=0.22)
 xgb = XGBClassifier(objective ='reg:linear', colsample_bytree = 0.6, learning_rate = 0.35,
max_depth = 7, n_estimators = 13)
 voting_clf = VotingClassifier(
 estimators=[('ada', ada),('GB', gbrt),('xgb',xgb)],
voting='hard')
 voting_clf.fit(X_train, Y_train)
 vo_score = cross_val_score(voting_clf,X_train,Y_train,cv=7 ).mean()
print(",교차 검증 점수 : {:.3f}".format(vo_score))
print(",훈련 세트 정확도: {:.3f}".format(voting_clf.score(X_train, Y_train)))
 print(",테스트 세트 정확도: {:.3f}".format(voting_clf.score(X_test, Y_test)))
 print('================================================')
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import torch.nn.functional as F
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy
from sklearn.metrics import confusion_matrix
from torch.utils.data import random_split, Dataset
from torch.utils.data.sampler import SubsetRandomSampler
import glob
from PIL import Image
import glob
import cv2
import gc<load_from_csv>
<load_from_csv><EOS> 
sub = pd.read_csv('.. /input/smhrd12/sample_submission.csv')
 sub['income'] = gbrt_pred
sub.to_csv('submission.csv',index=False )