Kaggle competition: Quick, Draw! Doodle Recognition Challenge
<SOS> metric: MAP@{K} Kaggle data source: quick,-draw!-doodle-recognition-challenge<init_hyperparams>
import torch.nn as nn
import torch
from torch.utils.data import Dataset, DataLoader, ConcatDataset
import torchvision
from torchvision import transforms, utils
import glob
import os
CFG = {
'folder' : 'effnetb5512',
'fold_num': [0],
'seed': 719,
'model_arch': 'tf_efficientnet_b5_ns',
'img_size': 512,
'epochs': 21,
'train_bs': 16,
'valid_bs': 16,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 5,
'used_epochs': [3,4,5],
'weights': [1,1,1]
}
CFG1 = {
'folder' : 'b6-whole',
'fold_num': [0],
'seed': 719,
'model_arch': 'tf_efficientnet_b6_ns',
'img_size': 256,
'epochs': 21,
'train_bs': 16,
'valid_bs': 16,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 5,
'used_epochs': [0,1,2,3,4,5,6,7,8,9,21,37],
'weights': [1,1,1,1,1,1,1,1,1,1,1,1]
}
CFG2 = {
'folder' : '08855cv',
'fold_num': [0],
'seed': 719,
'model_arch': 'tf_efficientnet_b6_ns',
'img_size': 512,
'epochs': 21,
'train_bs': 16,
'valid_bs': 16,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 5,
'used_epochs': [2],
'weights': [1,]
}
CFG3 = {
'folder' : 'b3-ns-512-512',
'fold_num': [0],
'seed': 719,
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 512,
'epochs': 21,
'train_bs': 16,
'valid_bs': 16,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 1,
'used_epochs': [1,2,3,4,5,6,7,8,9],
'weights': [1,1,1,1,1,1,1,1,1]
}
CFG4 = {
'folder' : 'testingcv',
'fold_num': [2],
'seed': 719,
'model_arch': 'tf_efficientnet_b2_ns',
'img_size': 256,
'epochs': 21,
'train_bs': 16,
'valid_bs': 16,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 1,
'used_epochs': [2],
'weights': [1]
}
CFGs = [CFG4]<load_from_csv>
en_dict = {}
path = '.. /input/quickdraw-doodle-recognition/train_simplified'
filenames = glob.glob(os.path.join(path, '*.csv'))
filenames[:5]
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()<count_values>
def encode_labels() :
counter = 0
for fn in filenames:
en_dict[fn[:-4].split('/')[-1].replace(' ', '_')] = counter
counter += 1
train.label.value_counts()<load_from_csv>
encode_labels()
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<categorify>
dec_dict = {v: k for k , v in en_dict.items() }
def decode_labels(label):
return dec_dict[label]
class CassavaDataset(Dataset):
def __init__(
 self, df, data_root, transforms=None, output_label=True 
):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
if self.output_label:
target = self.df.iloc[index]['label']
path = "{}/{}".format(self.data_root, self.df.iloc[index]['image_id'])
 img = get_img(path)
 if self.transforms:
img = self.transforms(image=img)['image']
if self.output_label == True:
return img, target
else:
return img<import_modules>
decode_labels(181 )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_train_transforms(size=CFG['img_size']):
return Compose([
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
ToTensorV2(p=1.0),
], p=1.)
def get_valid_transforms(size=CFG['img_size']):
return Compose([
CenterCrop(size,size, p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
def get_inference_transforms(size = CFG['img_size']):
return Compose([
CenterCrop(size,size),
Transpose(p=0.5),
OneOf([HorizontalFlip(p=0.5),
VerticalFlip(p=0.5)],p=0.5)
 ,
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)<choose_model_class>
def get_label(nfile):
return en_dict[nfile[:-4].split('/')[-1].replace(' ', '_')]
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<create_dataframe>
get_label(".. /input/quickdraw-doodle-recognition/train_simplified/The Eiffel Tower.csv" )
if __name__ == '__main__':
seed_everything(CFG['seed'])
 test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
final_vals = []
final_preds = []
for cfg in CFGs:
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms(cfg['img_size']), output_label=False)
 val_preds = []
tst_preds = []
tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 device = torch.device(CFG['device'])
 model = CassvaImgClassifier(cfg['model_arch'], train.label.nunique() ).to(device)
 for fold in cfg["fold_num"]:
tst_preds = []
for i, epoch in enumerate(cfg['used_epochs']):
model.load_state_dict(torch.load('.. /input/'+cfg["folder"]+'/{}_fold_{}_{}'.format(cfg['model_arch'],fold,epoch)))
 with torch.no_grad() :
for _ in range(CFG['tta']):
tst_preds += [cfg['weights'][i]/sum(cfg['weights'])/cfg['tta']*inference_one_epoch(model, tst_loader, device)]
tst_preds = np.mean(tst_preds, axis=0)
 try:
final_preds+=tst_preds
except:
final_preds = tst_preds
del model
torch.cuda.empty_cache() 
<save_to_csv>
class DoodleDataset(Dataset):
def __init__(self, csv_file, root_dir, mode='train', nrows=1000, skiprows=None, size=256, transform=None):
self.root_dir = root_dir
file = os.path.join(root_dir, csv_file)
 self.size=size
self.mode = mode
self.doodle = pd.read_csv(file, usecols=['drawing'], nrows=nrows, skiprows=skiprows)
 self.transform = transform
if self.mode == 'train':
self.label = get_label(csv_file)
 @staticmethod
def _draw(raw_strokes, size=256, lw=6, time_color=True):
BASE_SIZE = 256
img = np.zeros(( BASE_SIZE, BASE_SIZE),np.uint8)
 for t, stroke in enumerate(raw_strokes):
for i in range(len(stroke[0])- 1):
color = 255 - min(t,10)* 13 if time_color else 255
_ = cv2.line(img,(stroke[0][i], stroke[1][i]),(stroke[0][i+1], stroke[1][i+1]), color, lw)
 if size != BASE_SIZE:
return cv2.resize(img,(size, size))
else:
return img
def __len__(self):
return len(self.doodle)
 def __getitem__(self, index):
raw_strokes = ast.literal_eval(self.doodle.drawing[index])
 sample = self._draw(raw_strokes, size=self.size, lw=2, time_color=True)
 if self.transform:
sample = self.transform(sample)
 if self.mode == 'train':
return(sample[None]/255 ).astype('float32'), self.label
else:
return(sample[None]/255 ).astype('float32')
test['label'] = np.argmax(final_preds/len(CFGs), axis=1)
 print(test.head())
 test.to_csv('submission.csv', index=False )<set_options>
SIZE = 224
select_nrows= 10000
doodles = ConcatDataset([DoodleDataset(fn.split('/')[-1], path, mode='train', nrows=select_nrows, skiprows=None, size=SIZE, transform=None)for fn in filenames])
ImageFile.LOAD_TRUNCATED_IMAGES = True
warnings.simplefilter('ignore')
 %matplotlib inline<set_options>
print(len(doodles))
train_dataloader = DataLoader(doodles, batch_size=128, shuffle=True, num_workers=0 )
n_epochs = 10
n_patience = 5
n_folds = 3
train_bsize = 24
valid_bsize = 48
test_bsize = 48
seed = 42
effnet_output = {0: 1280, 1: 1280, 2: 1408, 3: 1536, 4: 1792, 5: 2048, 6: 2304, 7: 2560}
IMG_SIZE = 512
EFFNET_MODEL = 4
AUGMENTATION =[albumentations.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=15, border_mode=0, p=0.6),
albumentations.Flip(p=0.5),
albumentations.RandomRotate90(p=0.5),
albumentations.RandomBrightness(limit=0.2, p=0.6),
albumentations.RandomContrast(limit=0.2, p=0.6),
albumentations.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=20, val_shift_limit=20, p=0.6),
albumentations.CoarseDropout(max_holes=8, max_height=int(IMG_SIZE*0.2), max_width=int(IMG_SIZE*0.2), p=0.6),
albumentations.Cutout(num_holes=1, max_h_size=int(IMG_SIZE*0.33), max_w_size=int(IMG_SIZE*0.33), p=0.6)
 ]
IS_TTA = True
TTA = 5
UPSAMPLE = False
N_UPSAMPLE = 1
SCHEDULER_NAME = 'CosineAnnealingLR'
LOSS_FN_NAME = 'CrossEntropyLoss'
SMOOTHING = 0.05
DISPLAY_PLOT= True
def set_seed(seed):
np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
os.environ['PYTHONHASHSEED'] = str(seed)
 set_seed(seed )<define_variables>
def validation(get_loader,lossfn, scorefn):
model.eval()
loss, score = 0,0
vlen = len(get_loader)
 for X , y in get_loader:
X = X.to(device)
 y = y.to(device)
 output = model(X)
 loss += lossfn(output, y ).item()
score += scorefn(output, y)[0].item()
model.train()
return loss/vlen, score/vlen
path = '.. /input/cassava-leaf-disease-classification/'
trained_path = '.. /input/cassava-b4-512-final/'<load_from_csv>
def accuracy(output, target, topk=(3,)) :
with torch.no_grad() :
maxk = max(topk)
 batch_size = target.size(0)
 _, pred = output.topk(maxk, 1, True, True)
 pred = pred.t()
correct = pred.eq(target.view(1,-1 ).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1 ).float().sum(0,keepdim=True)
 res.append(correct_k.mul_(100.0/batch_size))
return res
df = pd.read_csv(path + 'train.csv')
 N_CLASSES = df.label.nunique()<normalization>
def mapk(output, target, k=3):

 with torch.no_grad() :
batch_size = target.size(0)
 _, pred = output.topk(k, 1, True, True)
 pred = pred.t()
correct = pred.eq(target.reshape(1, -1 ).expand_as(pred))
for i in range(k):
correct[i] = correct[i]*(k-i)
 score = correct[:k].reshape(-1 ).float().sum(0, keepdim=True)
 score.mul_(1.0 /(k * batch_size))
return score
class ClassificationDataset:
def __init__(self, image_paths, targets, resize, augmentations=None):
self.image_paths = image_paths
self.targets = targets
self.resize = resize
self.augmentations = augmentations
def __len__(self):
return len(self.image_paths)
 def __getitem__(self, item):
image = Image.open(self.image_paths[item])
 targets = self.targets[item]
if self.resize is not None:
image = image.resize(
(self.resize[1], self.resize[0]), resample=Image.BILINEAR 
)
 image = np.array(image)
 if self.augmentations is not None:
augmented = self.augmentations(image=image)
 image = augmented["image"]
image = np.transpose(image,(2, 0, 1)).astype(np.float32)
 return {
"image": torch.tensor(image),
"targets": torch.tensor(targets),
}
class ClassificationDataLoader:
def __init__(self, image_paths, targets, resize, augmentations=None):
self.image_paths = image_paths
self.targets = targets
self.resize = resize
self.augmentations = augmentations
self.dataset = ClassificationDataset(
 image_paths=self.image_paths,
targets=self.targets,
resize=self.resize,
augmentations=self.augmentations 
)
 def fetch(self, batch_size, num_workers, drop_last=False, shuffle=True, tpu=False):
sampler = None
data_loader = torch.utils.data.DataLoader(
 self.dataset,
batch_size=batch_size,
sampler=sampler,
drop_last=drop_last,
shuffle=shuffle,
num_workers=num_workers 
)
 return data_loader<normalization>
a = torch.randn(10,2,220,200)
 k=5
a[:k].view(-1 )
class Engine:
@staticmethod
def train(
 data_loader,
model,
optimizer,
device,
scheduler=None,
accumulation_steps=1,
fp16=True, 
):
losses = AverageMeter()
accuracies = AverageMeter()
final_predictions = []
model.train()
if accumulation_steps > 1:
optimizer.zero_grad()
if fp16:
scaler = torch.cuda.amp.GradScaler()
for b_idx, data in enumerate(data_loader):
for key, value in data.items() :
data[key] = value.to(device)
 if accumulation_steps == 1 and b_idx == 0:
optimizer.zero_grad()
if fp16:
with torch.cuda.amp.autocast() :
predictions, loss, accuracy = model(**data)
 else:
predictions, loss, accuracy = model(**data)
 predictions = predictions.detach().cpu().numpy()
final_predictions.append(predictions)
 with torch.set_grad_enabled(True):
if fp16:
scaler.scale(loss ).backward()
else:
loss.backward()
if(b_idx + 1)% accumulation_steps == 0:
if fp16:
scaler.step(optimizer)
 scaler.update()
else:
optimizer.step()
if scheduler is not None:
scheduler.step()
if b_idx > 0:
optimizer.zero_grad()
losses.update(loss.item() , data_loader.batch_size)
 accuracies.update(accuracy.item() , data_loader.batch_size)
 return final_predictions, losses.avg, accuracies.avg
@staticmethod
def evaluate(data_loader, model, device):
losses = AverageMeter()
accuracies = AverageMeter()
final_predictions = []
model.eval()
with torch.no_grad() :
for b_idx, data in enumerate(data_loader):
for key, value in data.items() :
data[key] = value.to(device)
 predictions, loss, accuracy = model(**data)
 predictions = predictions.detach().cpu().numpy()
final_predictions.append(predictions)
 losses.update(loss.item() , data_loader.batch_size)
 accuracies.update(accuracy.item() , data_loader.batch_size)
 return final_predictions, losses.avg, accuracies.avg
@staticmethod
def predict(data_loader, model, device):
model.eval()
final_predictions = []
with torch.no_grad() :
for b_idx, data in enumerate(data_loader):
for key, value in data.items() :
data[key] = value.to(device)
 predictions, _, _ = model(**data)
 predictions = predictions.detach().cpu().numpy()
final_predictions.append(predictions)
 return final_predictions<choose_model_class>
model = torchvision.models.resnet18(pretrained=True )
class EfficientNet(nn.Module):
def __init__(self, num_classes):
super(EfficientNet, self ).__init__()
self.base_model = timm.create_model(f"tf_efficientnet_b{str(EFFNET_MODEL)}_ns", pretrained=False)
 self.dropout = nn.Dropout(0.2)
 self.out = nn.Linear(
 in_features=effnet_output[EFFNET_MODEL],
out_features=num_classes,
bias=True 
)
 def forward(self, image, targets=None):
batch_size, _, _, _ = image.shape
x = self.base_model.forward_features(image)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(batch_size, -1)
 out = self.out(self.dropout(x))
return out, None, None<load_from_csv>
def squeeze_weights(m):
m.weight.data = m.weight.data.sum(dim=1)[:,None]
m.in_channels = 1
model.conv1.apply(squeeze_weights)
 num_classes = 340
model.fc = nn.Linear(512, out_features=num_classes, bias=True )
test = pd.read_csv(path + "sample_submission.csv" )<save_to_csv>
%%time
model(torch.randn(12,1,224,224)).size()
final_preds = None
for i in range(n_folds):
preds = predict(fold = i, apply_tta=IS_TTA)
 temp_preds = None
for p in preds:
if temp_preds is None:
temp_preds = p
else:
temp_preds = np.vstack(( temp_preds, p))
if final_preds is None:
final_preds = temp_preds
else:
final_preds += temp_preds
final_preds /= n_folds
final_preds = final_preds.argmax(axis=1)
 test.label = final_preds
test.to_csv('submission.csv', index=False )<import_modules>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 model = model.to(device )
print("Tensorflow version " + tf.__version__ )<define_variables>
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=0.002, amsgrad = True)
 scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[5000, 12000, 18000], gamma=0.5 )
AUTOTUNE = tf.data.experimental.AUTOTUNE
GCS_PATH = ".. /input/cassava-leaf-disease-classification"
GCS_PATH_STRATIFICATED =".. /input/cassava-recreate-stratificated-tfrecords"
REPLICAS = strategy.num_replicas_in_sync
BATCH_SIZE = 128
AUG_BATCH = BATCH_SIZE
IMAGE_SIZE = [512, 512]
DIM = IMAGE_SIZE[0]
CLASSES = ['0', '1', '2', '3', '4']
EPOCHS = 40
FOLDS = 5
debug = True
TTA = 2
WGTS = [1/FOLDS]*FOLDS
calc_oof = False<load_from_csv>
%%time
epochs = 1
lsize = len(train_dataloader)
 print(f"size of train : {lsize}")
 itr =1
p_itr = 1000
model.train()
tloss, score =0,0
for epoch in range(epochs):
for X, y in train_dataloader:
X, y = X.to(device), y.to(device)
 output = model(X)
 loss = criterion(output, y)
 tloss += loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
score += mapk(output, y)[0].item()
scheduler.step()
if itr %p_itr ==0 :
print('Iteration {} -> Train Loss: {:.4f}, MAP@3: {:.3f}'.format(itr, tloss/p_itr, score/p_itr))
tloss, score = 0, 0
itr +=1
if itr >= 6000:
break
test_df = pd.read_csv(GCS_PATH + '/sample_submission.csv')
 train_df = pd.read_csv(GCS_PATH + '/train.csv' )<sort_values>
filename_pth = 'checkpoint_resnet18.pth'
torch.save(model.state_dict() , filename_pth )
files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH + '/test_tfrecords/*.tfrec')) )<define_variables>
testset = DoodleDataset('test_simplified.csv', '.. /input/quickdraw-doodle-recognition/', mode='test', nrows=None, size=SIZE)
 testloader = DataLoader(testset, batch_size=128, shuffle=False, num_workers=0 )
ROT_ = 180.0
SHR_ = 2.0
HZOOM_ = 8.0
WZOOM_ = 8.0
HSHIFT_ = 8.0
WSHIFT_ = 8.0<normalization>
model.eval()
model = model.to(device)
 labels = np.empty(( 0,3))
for x in tqdm.tqdm(testloader):
x = x.to(device)
 output = model(x)
 _, pred = output.topk(3, 1, True, True)
 labels = np.concatenate([labels, pred.cpu() ], axis = 0 )
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform_mat(image, DIM=IMAGE_SIZE[0]):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d, [DIM, DIM,3] )<data_type_conversions>
%%time
submission = pd.read_csv('.. /input/quickdraw-doodle-recognition/test_simplified.csv', index_col='key_id')
 submission.drop(['countrycode', 'drawing'], axis=1, inplace=True)
 submission['word'] = ''
for i, label in enumerate(labels):
submission.word.iloc[i] = " ".join([decode_labels(l)for l in label] )
def to_float32(image, label):
return tf.cast(image, tf.float32), label<categorify>
submission.to_csv('submission.csv' )
<split><EOS> 
print("done" )