Kaggle competition: Digit Recognizer
def load_model(model_name,path):
if model_name == 'densenet121':
state_dict = torch.load(path, torch.device('cuda'))
model = class_densenet121_dropout(num_classes=19,in_channels=4,pretrained_file=None)
 model.cuda()
model.load_state_dict(state_dict)
 model.eval()
return model<define_variables>
Data=pd.read_csv('.. /input/digit-recognizer/train.csv' )
folds = [0,1,2,3,4]
model_dic = {'densenet121':'.. /input/hpafinal/output/run_nn_20210504_000509/'}<load_pretrained>
Y=np.array(Data['label'])
 X=np.array(Data.drop('label',axis=1)) / 255 .
rgby_models = []
for model_name in model_dic:
path = model_dic[model_name]
for fold in folds:
if os.path.exists(path+'fold%s.ckpt'%fold):
model = load_model(model_name,path+'fold%s.ckpt'%fold)
 rgby_models.append(model)
 print('daishu model count:', len(rgby_models))<categorify>
plt.imshow(X[25].reshape(28,28))
print(Y[25] )
def get_trans(img, I, mode='bgry'):
if mode == 'rgby':
img = img[:, [2,1,0,3]]
if I >= 4:
img = img.transpose(2,3)
 if I % 4 == 0:
return img
elif I % 4 == 1:
return img.flip(2)
 elif I % 4 == 2:
return img.flip(3)
 elif I % 4 == 3:
return img.flip(2 ).flip(3)
 def get_trans_daishu(img, I, mode='bgry'):
if mode == 'rgby':
img = img[:, [2,1,0,3]]
if I == 0:
img = img[:, :, 64:704, 64:704]
if I == 1:
img = img[:, :, :640, :640].flip(2)
 if I == 2:
img = img[:, :, :640, 128:].flip(3)
 if I == 3:
img = img[:, :, 128:, 128:].flip(2 ).flip(3)
 if I == 4:
img = img[:, :, 128:, :640].transpose(2,3)
 if I == 5:
img = img[:, :, 32:672, 96:736].transpose(2,3 ).flip(2)
 if I == 6:
img = img[:, :, 96:736, 32:672].transpose(2,3 ).flip(3)
 img = F.interpolate(img, size=[512, 512], mode="bilinear")
 return img<normalization>
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2 )
IDs = []
encs = []
PRED_FINAL = []
little_bs = 16
with torch.no_grad() :
for ID, enc, images in tqdm(dataloader):
try:
if len(enc[0])> 0:
with amp.autocast() :
for k in images.keys() :
images[k] = images[k].cuda()
if images[k].ndim == 5:
images[k] = images[k].squeeze(0)
 preds = {
'orig': [],
'cells': [],
}
for m, inp_types in zip(models, input_types):
for t in inp_types:
if t in ['masked', 'cells_128', 'cells_256']:
for I in np.random.choice(range(8), TTA[t], replace=False):
this_pred = torch.cat([
m(get_trans(images[t][b:b+little_bs], I)).sigmoid() \
for b in range(0, images[t].shape[0], little_bs)
 ])
 preds['cells'].append(this_pred)
 for m in rgby_models:
for I in np.random.choice(range(8), TTA['center_cells'], replace=False):
this_pred = torch.cat([
m(get_trans(images['center_cells'][b:b+little_bs], I, 'rgby')) [1].sigmoid() \
for b in range(0, images['center_cells'].shape[0], little_bs)
 ])
 preds['cells'].append(this_pred)
 for k in preds.keys() :
if len(preds[k])> 0:
preds[k] = torch.stack(preds[k], 0 ).mean(0)
 else:
preds[k] = 0
pred_final = preds['cells']
PRED_FINAL.append(pred_final.cpu())
 IDs += [ID[0]] * images['cells_128'].shape[0]
encs += enc[0].split(' ')
 except:
print('error')
 pass
PRED_FINAL = torch.cat(PRED_FINAL ).float()<categorify>
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten,BatchNormalization,Dropout,Conv2D,MaxPool2D
PredictionString = []
for i in tqdm(range(PRED_FINAL.shape[0])) :
enc = encs[i]
prob = PRED_FINAL[i]
sub_string = []
for cid, p in enumerate(prob):
sub_string.append(' '.join([str(cid), f'{p:.5f}', enc]))
sub_string = ' '.join(sub_string)
 PredictionString.append(sub_string )<create_dataframe>
print(tf.config.list_physical_devices('GPU'),'//',tf.test.is_built_with_cuda() )
df_pred = pd.DataFrame({
'ID': IDs,
'PredictionString': PredictionString
})
 df_pred = df_pred.groupby(['ID'])['PredictionString'].apply(lambda x: ' '.join(x)).reset_index()<save_to_csv>
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.12,
height_shift_range=0.12,
shear_range=0.12,
validation_split=0.2
)
df_sub = df_sub[['ID', 'ImageWidth', 'ImageHeight']].merge(df_pred, on='ID', how="left")
 df_sub.fillna('', inplace=True)
 df_sub.to_csv('submission.csv', index=False )<install_modules>
training_generator = datagen.flow(X_train, y_train, batch_size=32,subset='training')
 validation_generator = datagen.flow(X_train, y_train, batch_size=32,subset='validation' )
!pip install -q ".. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl"
!pip install -q ".. /input/hpapytorchzoozip/pytorch_zoo-master"
!pip install -q ".. /input/hpacellsegmentatormaster/HPA-Cell-Segmentation-master"
NUC_MODEL = '.. /input/hpacellsegmentatormodelweights/dpn_unet_nuclei_v1.pth'
CELL_MODEL = '.. /input/hpacellsegmentatormodelweights/dpn_unet_cell_3ch_v1.pth'<choose_model_class>
model=Sequential()
segmentator = cellseg.CellSegmentator(
 NUC_MODEL,
CELL_MODEL,
scale_factor=0.25,
padding=True,
multi_channel_model=True 
)
<set_options>
model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.3))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
gpus = tf.config.experimental.list_physical_devices('GPU')
 if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
 logical_gpus = tf.config.experimental.list_logical_devices('GPU')
 print(len(gpus), "...Physical GPUs,", len(logical_gpus), "Logical GPUs...
 ")
 except RuntimeError as e:
print(e )<load_pretrained>
model.compile(
 loss="sparse_categorical_crossentropy",
optimizer=tf.keras.optimizers.RMSprop(lr=0.003, rho=0.9, epsilon=1e-08, decay=0.0),
metrics=["accuracy"]
)
RGB_model = keras.models.load_model('.. /input/hpa-models-2021/ProteinModelRGB_rev_18.h5')
 G_model = keras.models.load_model('.. /input/hpa-models-2021/GreentileProteinModel_rev_2.h5')
 multicellmodel = keras.models.load_model('.. /input/hpa-models-2021/Full_image_greenModelRev9.h5', custom_objects={'FixedDropout':FixedDropout(rate=0.5)})
 img_type = 'g'
multicell2model = keras.models.load_model('.. /input/hpa-models-2021/Full_image_RYB_GModelRev11.h5', custom_objects={'FixedDropout':FixedDropout(rate=0.5)})
 img2_type = 'ryb_g'<categorify>
cb=tf.keras.callbacks.EarlyStopping(patience=10,restore_best_weights=True)
 learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
remove_small_holes, remove_small_objects)
 def label_cell(nuclei_pred, cell_pred):

 def __wsh(
 mask_img,
threshold,
border_img,
seeds,
threshold_adjustment=0.35,
small_object_size_cutoff=10, 
):
img_copy = np.copy(mask_img)
 m = seeds * border_img
img_copy[m <= threshold + threshold_adjustment] = 0
img_copy[m > threshold + threshold_adjustment] = 1
img_copy = img_copy.astype(np.bool)
 img_copy = remove_small_objects(img_copy, small_object_size_cutoff ).astype(
 np.uint8 
)
 mask_img[mask_img <= threshold] = 0
mask_img[mask_img > threshold] = 1
mask_img = mask_img.astype(np.bool)
 mask_img = remove_small_holes(mask_img, 63)
 mask_img = remove_small_objects(mask_img, 1 ).astype(np.uint8)
 markers = ndi.label(img_copy, output=np.uint32)[0]
labeled_array = segmentation.watershed(
 mask_img, markers, mask=mask_img, watershed_line=True 
)
 return labeled_array
nuclei_label = __wsh(
 nuclei_pred[..., 2] / 255.0,
0.4,
1 -(nuclei_pred[..., 1] + cell_pred[..., 1])/ 255.0 > 0.05,
nuclei_pred[..., 2] / 255,
threshold_adjustment=-0.25,
small_object_size_cutoff=32, 
)
 nuclei_label = remove_small_objects(nuclei_label, 157)
 nuclei_label = measure.label(nuclei_label)
 threshold_value = max(0.22, filters.threshold_otsu(cell_pred[..., 2] / 255)* 0.5)
 cell_region = np.multiply(
 cell_pred[..., 2] / 255 > threshold_value,
np.invert(np.asarray(cell_pred[..., 1] / 255 > 0.05, dtype=np.int8)) , 
)
 sk = np.asarray(cell_region, dtype=np.int8)
 distance = np.clip(cell_pred[..., 2], 255 * threshold_value, cell_pred[..., 2])
 cell_label = segmentation.watershed(-distance, nuclei_label, mask=sk)
 cell_label = remove_small_objects(cell_label, 344 ).astype(np.uint8)
 selem = disk(2)
 cell_label = closing(cell_label, selem)
 cell_label = __fill_holes(cell_label)
 sk = np.asarray(
 np.add(
 np.asarray(cell_label > 0, dtype=np.int8),
np.asarray(cell_pred[..., 1] / 255 > 0.05, dtype=np.int8), 
)
 > 0,
dtype=np.int8, 
)
 cell_label = segmentation.watershed(-distance, cell_label, mask=sk)
 cell_label = __fill_holes(cell_label)
 cell_label = np.asarray(cell_label > 0, dtype=np.uint8)
 cell_label = measure.label(cell_label)
 cell_label = remove_small_objects(cell_label, 344)
 cell_label = measure.label(cell_label)
 cell_label = np.asarray(cell_label, dtype=np.uint16)
 return nuclei_label, cell_label
def __fill_holes(image):

 boundaries = segmentation.find_boundaries(image)
 image = np.multiply(image, np.invert(boundaries))
image = ndi.binary_fill_holes(image > 0)
 image = ndi.label(image)[0]
return image<define_variables>
model.fit(training_generator,epochs=100,callbacks=[cb,learning_rate_reduction],validation_data=validation_generator )
def build_image_names(image_id: str)-> list:
mt = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_red.png'
er = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_yellow.png'
nu = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_blue.png'
high = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_green.png'
return [mt], [er], [nu], [high], [[mt], [er], [nu]],
def grab_contours(cnts):
if len(cnts)== 2:
cnts = cnts[0]
elif len(cnts)== 3:
cnts = cnts[1]
else:
raise Exception(( "Contours tuple must have length 2 or 3, "
"otherwise OpenCV changed their cv2.findContours return "
"signature yet again.Refer to OpenCV's documentation "
"in that case"))
return cnts
def create_cell_images(RGB, RYB_G, G, cell_masks, size):
def clipimgtosquare(group_img):
cnt_img = np.zeros_like(group_img)
 cnt_img[cover == 255] = group_img[cover == 255]
cnt_img = cnt_img[y:y+h, x:x+w]
old_size = cnt_img.shape[:2]
ratio = float(size)/max(old_size)
 new_size = tuple([int(x*ratio)for x in old_size])
 resized = cv2.resize(cnt_img,(new_size[1], new_size[0]))
delta_w = size - new_size[1]
delta_h = size - new_size[0]
top, bottom = delta_h//2, delta_h-(delta_h//2)
 left, right = delta_w//2, delta_w-(delta_w//2)
 color = [0, 0, 0]
square = cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
 square = img_to_array(square)
 return square
mask = cv2.convertScaleAbs(cell_masks)
 cnts = grab_contours(cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE))
RGBs = []
RYB_Gs = []
Gs = []
for i in range(1,cell_masks.max()):
mask = cv2.convertScaleAbs(np.where(cell_masks==i, 1, 0))
c = grab_contours(cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)) 
(x,y,w,h)= cv2.boundingRect(c[0])
 cover = np.zeros_like(RGB)
 cv2.drawContours(cover, [c[0]], 0,(255,255,255), -1)
 rgb = clipimgtosquare(RGB)
 ryb_g = clipimgtosquare(RYB_G)
 g = clipimgtosquare(G)
 RGBs.append(rgb)
 RYB_Gs.append(ryb_g)
 Gs.append(g)
 return RGBs, RYB_Gs, Gs
def image_predictions(images, model, TTArepeat=0, batch_size=8):
labels = []
confidences = []
images = np.vstack(images)
 confidence = model.predict(images,batch_size=batch_size)
 if TTArepeat > 0:
TTApred = []
TTApred.append(confidence)
 image = data_augmentation(images)
 for i in range(TTArepeat):
image = data_augmentation(image)
 TTApred.append(model.predict(image))
confidence = np.mean(TTApred,axis=0)
 confidences.append(confidence)
 return confidences
def create_cell_masks(mask):

 if mask.dtype != np.bool:
raise ValueError(
 "encode_binary_mask expects a binary mask, received dtype == %s" %
mask.dtype)
 mask = np.squeeze(mask)
 if len(mask.shape)!= 2:
raise ValueError(
 "encode_binary_mask expects a 2d mask, received shape == %s" %
mask.shape)
 mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
 mask_to_encode = mask_to_encode.astype(np.uint8)
 mask_to_encode = np.asfortranarray(mask_to_encode)
 encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]
binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
 base64_str = base64.b64encode(binary_str)
 return base64_str
def get_image_masks(image_id):
mt, er, nu, high, images = build_image_names(image_id=image_id)
 nuc_segmentations = segmentator.pred_nuclei(images[2])
 cell_segmentations = segmentator.pred_cells(images)
 nuclei_mask, cell_mask = label_cell(nuc_segmentations[0], cell_segmentations[0])
 blue = normalization(plt.imread(nu[0]))
green = normalization(plt.imread(high[0]))
red = normalization(plt.imread(mt[0]))
yellow = normalization(plt.imread(er[0]))
RGB = np.dstack(( red, green, blue))
RYB_G =img = np.dstack(( red+green, yellow+green, blue+green))
G = np.stack(( green,)*3, axis=-1)
 return RGB, RYB_G, G, cell_mask
def flatten_list_of_lists(l_o_l, to_string=False):
if not to_string:
return [item for sublist in l_o_l for item in sublist]
else:
return [str(item)for sublist in l_o_l for item in sublist]
def image_prediction_string(confidences, masks, labelqty = 19, threshold =.00):
labels = []
probs = []
codes = []
predictionstring = []
for pred, mask in zip(confidences, masks):
neglabel = 1-pred.max()
for label in range(0,labelqty):
if pred[label]>threshold:
labels.append(label)
 probs.append(pred[label])
 codes.append(mask.decode('UTF-8'))
labels.append(labelqty)
 probs.append(neglabel)
 codes.append(mask.decode('UTF-8'))
predictionstring = [" ".join(flatten_list_of_lists(zip([label, pred, mask]), to_string=True)) for label, pred, mask in zip(labels, probs, codes)]
return(" ".join(predictionstring))
def data_aug_exp(img, modeltype, size):
images = []
images.append(img)
 image = tf.image.rot90(img, k=1)
 images.append(image)
 image = tf.image.central_crop(image, central_fraction=.6)
 image = tf.image.resize(image, [size,size])
 images.append(image)
 image = tf.image.flip_left_right(img)
 images.append(image)
 image = tf.image.central_crop(image, central_fraction=.6)
 image = tf.image.resize(image, [size,size])
 images.append(image)
 image = tf.image.flip_up_down(img)
 images.append(image)
 image = tf.image.central_crop(image, central_fraction=.6)
 image = tf.image.resize(image, [size,size])
 images.append(image)
 if modeltype == 'ryb_g':
image = tf.image.adjust_contrast(img, 0.55)
 images.append(image)
 images = tf.expand_dims(images, axis=0)
 images = np.vstack(images)
 return images
def image_predictions_exp(images, model, modeltype, TTArepeat=False, batch_size=8, size=128):
images = np.expand_dims(images, axis=0)
 images = np.vstack(images)
 confidence = model.predict(images,batch_size=batch_size)
 if TTArepeat:
TTApred = []
aug_images = data_aug_exp(images,modeltype,size)
 for img in aug_images:
TTApred.append(model.predict(img))
confidence = np.mean(TTApred,axis=0)
 return confidence
def weighted_predictions(pred1, wt1, pred2=0, wt2=0, pred3=0, wt3=0):
new_pred = pred1*wt1+pred2*wt2+pred3*wt3
return np.array(new_pred)
 def normalization(array):
a =(array - array.min())/(array.max() -array.min())
 return a
def add_neglabel(array, labelqty):
arraynew = []
maximum = array.max(axis = 1)
 array = np.insert(array, labelqty, 1-maximum, axis = 1)
 return array
def print_cell(image, title, index):
plt.subplot(1,10,index)
 plt.imshow(image)
 plt.title(title)
 plt.axis('off')
<define_variables>
model.evaluate(X_test, y_test )
start = time.time()
test_dir = '.. /input/hpa-single-cell-image-classification/test/'
test_images = os.listdir(test_dir)
 images = [i.split("_")[0] for i in test_images]
names = np.unique(images)
 public = len(names)==559
if public:
print('...only public testset...')
 names = names[0:2]<save_to_csv>
pred_Data=np.array(pd.read_csv('.. /input/digit-recognizer/test.csv')/ 255.)
X_pred=pred_Data.reshape(( -1,28,28,1))
sub.to_csv("/kaggle/working/submission.csv", index=False )<import_modules>
predictions=model.predict_classes(X_pred )
<prepare_x_and_y><EOS> 
submit=pd.DataFrame({'ImageId':range(1,len(predictions)+1),'Label':predictions})
 submit.to_csv('submission.csv',index=False )