Kaggle competition: Digit Recognizer
threshold = 0.0
for i in range(pred_torch.shape[0]):
p = torch.nonzero(pred_torch[i] > threshold ).squeeze().numpy().tolist()
if type(p)!= list: p = [p]
if len(p)== 0: cls = [(pred_torch[i].argmax().item() , pred_torch[i].max().item())]
else: cls = [(x, pred_torch[i][x].item())for x in p]
cell_df['cls'].loc[i] = cls<categorify>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from sklearn.model_selection import train_test_split
def combine(r):
cls = r[0]
enc = r[1]
classes = [str(c[0])+ ' ' + str(c[1])+ ' ' + enc for c in cls]
return ' '.join(classes)
 combine(cell_df[['cls', 'enc']].loc[24]);<feature_engineering>
training = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 testing = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
cell_df['pred'] = cell_df[['cls', 'enc']].apply(combine, axis=1)
 cell_df.head()<groupby>
X = training.drop(columns = 'label' ).values
y = training['label'].values
subm = cell_df.groupby(['image_id'])['pred'].apply(lambda x: ' '.join(x)).reset_index()
subm.head()<load_from_csv>
X = X/255
X = X.reshape(X.shape[0], *(28, 28, 1))
print(X.shape )
sample_submission = pd.read_csv('.. /input/hpa-single-cell-image-classification/sample_submission.csv')
 sample_submission.head()<merge>
testing = testing.values
testing = testing/255
testing = testing.reshape(testing.shape[0], *(28, 28, 1))
sub = pd.merge(
 sample_submission,
subm,
how="left",
left_on='ID',
right_on='image_id', 
)
 sub.head()<feature_engineering>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, random_state = 0 )
def isNaN(num):
return num != num
for i, row in sub.iterrows() :
if isNaN(row['pred']): continue
sub.PredictionString.loc[i] = row['pred']<save_to_csv>
cnn = tf.keras.models.Sequential()
sub.to_csv('submission.csv', index=False )<save_to_csv>
cnn.add(layer = tf.keras.layers.Conv2D(input_shape =(28, 28, 1),
filters=64,
kernel_size =(3,3),
activation = 'relu'))
sub.to_csv('submission.csv', index=False )<load_from_csv>
cnn.add(layer = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides = 2))
!rm cell_df.csv<install_modules>
cnn.add(layer = tf.keras.layers.Conv2D(filters=64,
kernel_size =(3,3),
activation = 'relu'))
!pip install ".. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl"
!pip install ".. /input/hpapytorchzoozip/pytorch_zoo-master"
!pip install ".. /input/hpacellsegmentatorraman/HPA-Cell-Segmentation/"
!pip install ".. /input/efficientnet-keras-source-code/" --no-deps
!pip install ".. /input/kerasapplications" 
<install_modules>
cnn.add(layer = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides = 2))
!pip install ".. /input/efficientnet-pytorch-07/efficientnet_pytorch-0.7.0" 
<set_options>
cnn.add(layer = tf.keras.layers.Flatten() )
gpus = tf.config.list_physical_devices('GPU')
 if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_virtual_device_configuration(
 gpu,
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=5600)])
 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_from_csv>
cnn.add(layer = tf.keras.layers.Dropout(0.5))
data_df_sample_submission = pd.read_csv('.. /input/hpa-single-cell-image-classification/sample_submission.csv')
 test_files = os.listdir(".. /input/hpa-single-cell-image-classification/test")
 color_list = ["_red.png", "_green.png", "_yellow.png", "_blue.png"]
test_files_names = [re.sub(r'|'.join(map(re.escape, color_list)) , '', elem)for elem in test_files]
test_files_names = list(set(test_files_names))
d = []
for i, file in enumerate(test_files_names):
img = cv2.imread(f".. /input/hpa-single-cell-image-classification/test/{file}_red.png")
 height, width, channels = img.shape
d.append({
"ID" : file,
"ImageWidth" : width,
"ImageHeight": height,
"PredictionString" : "0 1 eNoLCAgIMAEABJkBdQ=="
})
 if i%50 == 0:
print(i)
 df_from_files = pd.DataFrame(d ).sort_values("ID" ).reset_index(drop=True)
 assert all(df_from_files == data_df_sample_submission), "Dataframes do not match"<define_variables>
cnn.add(layer = tf.keras.layers.Dense(units = 108, activation='relu'))
PATH_TEST = ".. /input/hpa-single-cell-image-classification/test"
MODEL_PATH = ".. /input/models-hpa/"
MODELS_LIST_EFFNET = [
"efficientnet-b4_rgby_lr_0.001_ADAM_steplr_g085_focal1_g1.0_resize640_mediumaug_3.pth",
"efficientnet-b4_rgby_lr_0.0015_ADAM_focal1_g1.0_resize640_10pcTest_HEAVY_AUG_E3_F0.pth",
"efficientnet-b4_rgby_lr_0.001_ADAM_steplr_g085_focal1_g1.0_resize640_mediumaug_E4_F4.pth",
]
MODELS_LIST_RESNEST = [
"resnest101_rgby_lr_0.002_SGD_polyoptim_focal1_g1.0_scheduler_largedataset_resize640_mediumaug_3.pth",
"resnest101_rgby_lr_0.002_SGD_focal1_g1.0_resize640_5pcTest_BS8_E3_F0.pth",
]
MODEL_LABELS_EFF = ".. /input/hpa-tensorflow-models/model_green.06-0.07.h5"
MODEL_LABELS_RN = ".. /input/hpa-tensorflow-models/model_rgb_resnext101.09-0.10.h5"
MODEL_LABELS_VIT = ".. /input/hpa-tensorflow-models/ggg_ViTB16_RedPlat_ADAMW_BCE_EPOCH12-VAL0.0957.h5" 
<init_hyperparams>
cnn.add(layer = tf.keras.layers.Dropout(0.5))
class CFG:
debug=False
verbose = False
num_workers=8
model_name_effnet = 'efficientnet-b4'
model_name_resnest = 'resnest101'
size=640
seed=2002
classes = 19
color_mode = "rgby"
resnest = True
effnet = True
extra_model_for_labels = True
extra_model_is_tf = True
only_green_extra_model = True
color_mode_image_level = "rgb"
split = [0.6, 0.4, 0]
size_seg = None
split_image_level = [0.33, 0.33, 0.34, 0]
split_cam_level = [0.5, 0.5]
split_sigmoid_graboost = [0.5, 0.5]
sigmoid_factor = 2.0
sigmoid_move = 0.2
is_demo = len(data_df)==559
if CFG.is_demo:
data_df = data_df[:10]
batch_size_ = 4<normalization>
cnn.add(layer = tf.keras.layers.Dense(units = 108, activation='relu'))
Compose, OneOf, Normalize, Resize, RandomResizedCrop, RandomCrop, HorizontalFlip, VerticalFlip,
RandomBrightness, RandomContrast, RandomBrightnessContrast, Rotate, ShiftScaleRotate, Cutout,
IAAAdditiveGaussianNoise, Transpose, HueSaturationValue, CoarseDropout 
)
 dataset_mean = [0.0994, 0.0466, 0.0606, 0.0879]
dataset_std = [0.1406, 0.0724, 0.1541, 0.1264]
def get_transforms(*, data_type):
if data_type == 'valid':
return Compose([
Resize(CFG.size, CFG.size),
ToTensorV2() ,
])
 elif data_type == 'test_green_model':
return Compose([
Resize(600, 600),
])
 elif data_type == 'test_green_model_torch':
return Compose([
Resize(CFG.size, CFG.size),
ToTensorV2() ,
] )<load_pretrained>
cnn.add(layer = tf.keras.layers.Dense(units = 10, activation = 'softmax'))
def load_RGBY_image(image_id, path, mode="cam", image_size=None):
if mode == "green_model":
green = read_img_scale255(image_id, "green",path, image_size)
 stacked_images = np.transpose(np.array([green, green, green]),(1,2,0))
return stacked_images
if mode=="cam":
red = read_img(image_id, "red", path, image_size)
 green = read_img(image_id, "green",path, image_size)
 blue = read_img(image_id, "blue",path, image_size)
 yellow = read_img(image_id, "yellow",path, image_size)
 if CFG.color_mode == "rgby":
stacked_images = np.transpose(np.array([red, green, blue,yellow]),(1,2,0))
else:
stacked_images = np.transpose(np.array([red, green, blue]),(1,2,0))
return stacked_images
def read_img(image_id, color, path, image_size=None):
filename = f'{path}/{image_id}_{color}.png'
assert os.path.exists(filename), f'not found {filename}'
img = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
 if image_size is not None:
img = cv2.resize(img,(image_size, image_size))
if img.max() > 255:
img_max = img.max()
img =(img/255 ).astype('uint8')
 return img
def read_img_scale255(image_id, color, path, image_size=None):
filename = f'{path}/{image_id}_{color}.png'
assert os.path.exists(filename), f'not found {filename}'
img = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
 if image_size is not None:
img = cv2.resize(img,(image_size, image_size))
if img.max() > 255:
img_max = img.max()
img =(img/255 ).astype('uint8')/255
return img
def one_hot_embedding(label, classes):
vector = np.zeros(( classes), dtype = np.float32)
 if len(label)> 0:
vector[label] = 1.
 return vector
class HPADataset_Test(Dataset):
def __init__(self, ids, path=None, transforms=None, mode="cam"):
self.ids = ids
self.transforms = transforms
self.mode = mode
self.path = path
def __len__(self):
return len(self.ids)
 def __getitem__(self, idx):
_ids = self.ids.iloc[idx]
image = load_RGBY_image(_ids, self.path, self.mode)
 if self.transforms:
augmented = self.transforms(image=image)
 image = augmented['image']
return image, _ids<categorify>
cnn.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'] )
def build_decoder(with_labels=True, target_size=(300, 300), ext='jpg'):
def decode(path):
if CFG.color_mode_image_level == "ggg":
file_bytes = tf.io.read_file(path + "_green.png")
 if ext == 'png':
img = tf.image.decode_png(file_bytes, channels=3)
 elif ext in ['jpg', 'jpeg']:
img = tf.image.decode_jpeg(file_bytes, channels=3)
 else:
raise ValueError("Image extension not supported")
 img = tf.cast(img, tf.float32)/ 255.0
img = tf.image.resize(img, target_size)
 return img
if CFG.color_mode_image_level == "rgb":
r = tf.io.read_file(path + "_red.png")
 g = tf.io.read_file(path + "_green.png")
 b = tf.io.read_file(path + "_blue.png")
 red = tf.io.decode_png(r, channels=1)
 blue = tf.io.decode_png(g, channels=1)
 green = tf.io.decode_png(b, channels=1)
 red = tf.image.resize(red, target_size)
 blue = tf.image.resize(blue, target_size)
 green = tf.image.resize(green, target_size)
 img = tf.stack([red, green, blue], axis=-1)
 img = tf.squeeze(img)
 img = tf.image.convert_image_dtype(img, tf.float32)/ 255
return img
def decode_with_labels(path, label):
return decode(path), label
return decode_with_labels if with_labels else decode
def build_augmenter(with_labels=True):
def augment(img):
img = tf.image.random_flip_left_right(img)
 img = tf.image.random_flip_up_down(img)
 return img
def augment_with_labels(img, label):
return augment(img), label
return augment_with_labels if with_labels else augment
def build_dataset_tf(paths, labels=None, bsize=32, cache=True,
decode_fn=None, augment_fn=None,
augment=True, repeat=True, shuffle=1024, img_size=300,
cache_dir=""):
if cache_dir != "" and cache is True:
os.makedirs(cache_dir, exist_ok=True)
 if decode_fn is None:
decode_fn = build_decoder(labels is not None, target_size=(img_size, img_size))
if augment_fn is None:
augment_fn = build_augmenter(labels is not None)
 AUTO = tf.data.experimental.AUTOTUNE
slices = paths if labels is None else(paths, labels)
 dset = tf.data.Dataset.from_tensor_slices(slices)
 dset = dset.map(decode_fn, num_parallel_calls=AUTO)
 dset = dset.cache(cache_dir)if cache else dset
dset = dset.map(augment_fn, num_parallel_calls=AUTO)if augment else dset
dset = dset.repeat() if repeat else dset
dset = dset.shuffle(shuffle)if shuffle else dset
dset = dset.batch(bsize ).prefetch(AUTO)
 return dset
test_paths = PATH_TEST + "/" + data_df['ID']
test_decoder_600 = build_decoder(with_labels=False, target_size=(600, 600))
test_decoder_384 = build_decoder(with_labels=False, target_size=(384, 384))
CFG.color_mode_image_level = "ggg"
dtest_tf_green_600 = build_dataset_tf(
 test_paths, bsize=batch_size_, repeat=False,
shuffle=False, augment=False, cache=False,
decode_fn=test_decoder_600 
)
 CFG.color_mode_image_level = "rgb"
dtest_tf_rgb_600 = build_dataset_tf(
 test_paths, bsize=batch_size_, repeat=False,
shuffle=False, augment=False, cache=False,
decode_fn=test_decoder_600 
)
 CFG.color_mode_image_level = "ggg"
dtest_tf_ggg_384 = build_dataset_tf(
 test_paths, bsize=batch_size_, repeat=False,
shuffle=False, augment=False, cache=False,
decode_fn=test_decoder_384
)<prepare_x_and_y>
model_train = cnn.fit(x = X_train, y = y_train, validation_data =(X_val, y_val), epochs = 25 )
class Yield_Images_Dataset(Dataset):
def __init__(self, csv_file, root=PATH_TEST, transform=None):
self.images_df = csv_file
self.transform = transform
self.root = root
def __len__(self):
return len(self.images_df)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
_id = self.images_df["ID"].iloc[idx]
r = os.path.join(self.root, f'{_id}_red.png')
 y = os.path.join(self.root, f'{_id}_yellow.png')
 b = os.path.join(self.root, f'{_id}_blue.png')
 r = cv2.imread(r, 0)
 y = cv2.imread(y, 0)
 b = cv2.imread(b, 0)
 size = r.shape[0]
if CFG.size_seg == None:
ryb_image = np.stack(( r, y, b), axis=2)/255.
 blue_image = b/255.
 return blue_image, ryb_image, size, _id
if size != CFG.size_seg:
blue_image = cv2.resize(b,(CFG.size_seg, CFG.size_seg)) /255.
 ryb_image = np.stack(( r, y, b), axis=2)
 ryb_image = cv2.resize(ryb_image,(CFG.size_seg, CFG.size_seg)) /255.
 else:
ryb_image = np.stack(( r, y, b), axis=2)/255.
 blue_image = b/255.
 return blue_image, ryb_image, size, _id 
<set_options>
y_pred = np.argmax(cnn.predict(testing), axis = -1 )
NUCLEI_MODEL_URL, TWO_CHANNEL_CELL_MODEL_URL)
 NORMALIZE = {"mean": [124 / 255, 117 / 255, 104 / 255], "std": [1 /(0.0167 * 255)] * 3}
class CellSegmentator(object):

 def __init__(
 self,
nuclei_model="./nuclei_model.pth",
cell_model="./cell_model.pth",
model_width_height=None,
device="cuda",
multi_channel_model=True,
return_without_scale_restore=False,
scale_factor=0.25,
padding=False 
):
if device != "cuda" and device != "cpu" and "cuda" not in device:
raise ValueError(f"{device} is not a valid device(cuda/cpu)")
 if device != "cpu":
try:
assert torch.cuda.is_available()
except AssertionError:
print("No GPU found, using CPU.", file=sys.stderr)
 device = "cpu"
self.device = device
if isinstance(nuclei_model, str):
if not os.path.exists(nuclei_model):
print(
 f"Could not find {nuclei_model}.Downloading it now",
file=sys.stderr, 
)
 download_with_url(NUCLEI_MODEL_URL, nuclei_model)
 nuclei_model = torch.load(
 nuclei_model, map_location=torch.device(self.device)
)
 if isinstance(nuclei_model, torch.nn.DataParallel)and device == "cpu":
nuclei_model = nuclei_model.module
self.nuclei_model = nuclei_model.to(self.device)
 self.multi_channel_model = multi_channel_model
if isinstance(cell_model, str):
if not os.path.exists(cell_model):
print(
 f"Could not find {cell_model}.Downloading it now", file=sys.stderr 
)
 if self.multi_channel_model:
download_with_url(MULTI_CHANNEL_CELL_MODEL_URL, cell_model)
 else:
download_with_url(TWO_CHANNEL_CELL_MODEL_URL, cell_model)
 cell_model = torch.load(cell_model, map_location=torch.device(self.device))
self.cell_model = cell_model.to(self.device)
 self.model_width_height = model_width_height
self.return_without_scale_restore = return_without_scale_restore
self.scale_factor = scale_factor
self.padding = padding
def _image_conversion(self, images):
microtubule_imgs, er_imgs, nuclei_imgs = images
if self.multi_channel_model:
if not isinstance(er_imgs, list):
raise ValueError("Please speicify the image path(s)for er channels!")
 else:
if not er_imgs is None:
raise ValueError(
 "second channel should be None for two channel model predition!" 
)
 if not isinstance(microtubule_imgs, list):
raise ValueError("The microtubule images should be a list")
 if not isinstance(nuclei_imgs, list):
raise ValueError("The microtubule images should be a list")
 if er_imgs:
if not len(microtubule_imgs)== len(er_imgs)== len(nuclei_imgs):
raise ValueError("The lists of images needs to be the same length")
 else:
if not len(microtubule_imgs)== len(nuclei_imgs):
raise ValueError("The lists of images needs to be the same length")
 if not all(isinstance(item, np.ndarray)for item in microtubule_imgs):
microtubule_imgs = [
os.path.expanduser(item)for _, item in enumerate(microtubule_imgs)
 ]
nuclei_imgs = [
os.path.expanduser(item)for _, item in enumerate(nuclei_imgs)
 ]
microtubule_imgs = list(
 map(lambda item: imageio.imread(item), microtubule_imgs)
)
 nuclei_imgs = list(map(lambda item: imageio.imread(item), nuclei_imgs))
if er_imgs:
er_imgs = [os.path.expanduser(item)for _, item in enumerate(er_imgs)]
er_imgs = list(map(lambda item: imageio.imread(item), er_imgs))
if not er_imgs:
er_imgs = [
np.zeros(item.shape, dtype=item.dtype)
 for _, item in enumerate(microtubule_imgs)
 ]
cell_imgs = list(
 map(
 lambda item: np.dstack(( item[0], item[1], item[2])) ,
list(zip(microtubule_imgs, er_imgs, nuclei_imgs)) , 
)
)
 return cell_imgs
def _pad(self, image):
rows, cols = image.shape[:2]
self.scaled_shape = rows, cols
img_pad= cv2.copyMakeBorder(
 image,
32, 
(32 - rows % 32),
32, 
(32 - cols % 32),
cv2.BORDER_REFLECT, 
)
 return img_pad
def pred_nuclei(self, images):
def _preprocess(images):
if isinstance(images[0], str):
raise NotImplementedError('Currently the model requires images as numpy arrays, not paths.')
 self.target_shapes = [image.shape for image in images]
if self.model_width_height:
images = np.array([transform.resize(image,(self.model_width_height,self.model_width_height))
for image in images])
 else:
images = [transform.rescale(image, self.scale_factor)for image in images]
if self.padding:
images = [self._pad(image)for image in images]
nuc_images = np.array([np.dstack(( image[..., 2], image[..., 2], image[..., 2])) if len(image.shape)>= 3
else np.dstack(( image, image, image)) for image in images])
 nuc_images = nuc_images.transpose([0, 3, 1, 2])
 return nuc_images
def _segment_helper(imgs):
with torch.no_grad() :
mean = torch.as_tensor(NORMALIZE["mean"], device=self.device)
 std = torch.as_tensor(NORMALIZE["std"], device=self.device)
 imgs = torch.tensor(imgs ).float()
imgs = imgs.to(self.device)
 imgs = imgs.sub_(mean[:, None, None] ).div_(std[:, None, None])
 imgs = self.nuclei_model(imgs)
 imgs = F.softmax(imgs, dim=1)
 return imgs
preprocessed_imgs = _preprocess(images)
 predictions = _segment_helper(preprocessed_imgs)
 predictions = predictions.to("cpu" ).numpy()
predictions = [self._restore_scaling(util.img_as_ubyte(pred), target_shape)
 for pred, target_shape in zip(predictions, self.target_shapes)]
return predictions
def _restore_scaling(self, n_prediction, target_shape):

 n_prediction = n_prediction.transpose([1, 2, 0])
 if self.padding:
n_prediction = n_prediction[
32 : 32 + self.scaled_shape[0], 32 : 32 + self.scaled_shape[1],...
 ]
n_prediction[..., 0] = 0
if not self.return_without_scale_restore:
n_prediction = cv2.resize(
 n_prediction, 
(target_shape[0], target_shape[1]),
interpolation=cv2.INTER_AREA, 
)
 return n_prediction
def pred_cells(self, images, precombined=False):
def _preprocess(images):
self.target_shapes = [image.shape for image in images]
for image in images:
if not len(image.shape)== 3:
raise ValueError("image should has 3 channels")
 if self.model_width_height:
images = np.array([transform.resize(image,(self.model_width_height,self.model_width_height))
for image in images])
 else:
images = np.array([transform.rescale(image, self.scale_factor, multichannel=True)for image in images])
 if self.padding:
images = np.array([self._pad(image)for image in images])
 cell_images = images.transpose([0, 3, 1, 2])
 return cell_images
def _segment_helper(imgs):
with torch.no_grad() :
mean = torch.as_tensor(NORMALIZE["mean"], device=self.device)
 std = torch.as_tensor(NORMALIZE["std"], device=self.device)
 imgs = torch.tensor(imgs ).float()
imgs = imgs.to(self.device)
 imgs = imgs.sub_(mean[:, None, None] ).div_(std[:, None, None])
 imgs = self.cell_model(imgs)
 imgs = F.softmax(imgs, dim=1)
 return imgs
if not precombined:
images = self._image_conversion(images)
 preprocessed_imgs = _preprocess(images)
 predictions = _segment_helper(preprocessed_imgs)
 predictions = predictions.to("cpu" ).numpy()
predictions = [self._restore_scaling(util.img_as_ubyte(pred), target_shape)
 for pred, target_shape in zip(predictions, self.target_shapes)]
return predictions<define_variables>
predictions = pd.DataFrame({"ImageId" : list(range(1, len(y_pred)+1)) ,
"Label" : y_pred} )
<define_variables><EOS> 
predictions.to_csv('predictions.csv', index = False )