Kaggle competition: Bengali.AI Handwritten Grapheme Classification
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='' )<import_modules>
PATH = '/kaggle/input/bengali-grapheme-second-try'
print('Tensorflow version ' + tf.__version__)
<set_options>
class MyRn50(nn.Module):
def __init__(self):
super(MyRn50, self ).__init__()
self.model_resnet = torchvision.models.resnet50()
num_ftrs = self.model_resnet.fc.in_features
self.model_resnet.fc = nn.Identity()
self.fc_graph = nn.Linear(num_ftrs, 168)
 self.fc_vowel = nn.Linear(num_ftrs, 11)
 self.fc_conso = nn.Linear(num_ftrs, 7)
 def forward(self, x):
x = self.model_resnet(x)
 out1 = self.fc_graph(x)
 out2 = self.fc_vowel(x)
 out3 = self.fc_conso(x)
 return out1, out2, out3
mymodel = MyRn50()
mymodel;
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
REPLICAS = strategy.num_replicas_in_sync
print("REPLICAS: ", REPLICAS )<define_variables>
model=MyRn50()
weights = torch.load('/kaggle/input/bgraph-rn50-weights/model_wts1.pth',
map_location=torch.device('cpu'))
model.load_state_dict(weights['state_dict'])
 model.eval() ;
GCS_DS_PATH = KaggleDatasets().get_gcs_path("tpu-getting-started")
 EXT_GCS = KaggleDatasets().get_gcs_path("tf-flower-photo-tfrec" )<define_variables>
class GraphemeDataset(Dataset):
def __init__(self, fname):
self.df = pd.read_parquet(fname)
 self.data = 255 - self.df.iloc[:, 1:].values.reshape(-1, HEIGHT, WIDTH ).astype(np.uint8)
 def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
name = self.df.iloc[idx,0]
img =(self.data[idx]*(255.0/self.data[idx].max())).astype(np.uint8)
 img = crop_resize(img)
 img =(img.astype(np.float32)/255.0 - imagenet_stats[0][0])/imagenet_stats[1][0]
return img, name
AUTO = tf.data.experimental.AUTOTUNE
IMAGE_SIZE = [331, 331]
EPOCHS = 25
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
SUBDIR = f"/tfrecords-jpeg-{IMAGE_SIZE[0]}x{IMAGE_SIZE[1]}"
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_DS_PATH + SUBDIR + "/train/*.tfrec")
 TEST_FILENAMES = tf.io.gfile.glob(GCS_DS_PATH + SUBDIR + "/test/*.tfrec")
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_DS_PATH + SUBDIR + "/val/*.tfrec")
 imagenet_files = tf.io.gfile.glob(EXT_GCS + '/imagenet' + SUBDIR + '/*.tfrec')
 inaturelist_files = tf.io.gfile.glob(EXT_GCS + '/inaturalist' + SUBDIR + '/*.tfrec')
 openimage_files = tf.io.gfile.glob(EXT_GCS + '/openimage' + SUBDIR + '/*.tfrec')
 oxford_files = tf.io.gfile.glob(EXT_GCS + '/oxford_102' + SUBDIR + '/*.tfrec')
 tensorflow_files = tf.io.gfile.glob(EXT_GCS + '/tf_flowers' + SUBDIR + '/*.tfrec')
 TRAINING_FILENAMES.extend(imagenet_files + inaturelist_files + \
openimage_files + oxford_files + tensorflow_files )<normalization>
row_id,target,bs = [],[],128
for fname in TEST:
ds = GraphemeDataset(fname)
 dl = DataLoader(ds, batch_size=bs, num_workers=nworkers, shuffle=False)
 with torch.no_grad() :
for x,y in dl:
x = x.unsqueeze(1)
 x = x.repeat(1,3,1,1)
 p1,p2,p3 = model(x)
 p1 = p1.argmax(-1 ).view(-1 ).cpu()
p2 = p2.argmax(-1 ).view(-1 ).cpu()
p3 = p3.argmax(-1 ).view(-1 ).cpu()
for idx,name in enumerate(y):
row_id += [f'{name}_grapheme_root',f'{name}_vowel_diacritic',
f'{name}_consonant_diacritic']
target += [p1[idx].item() ,p2[idx].item() ,p3[idx].item() ]
sub_df = pd.DataFrame({'row_id': row_id, 'target': target})
 sub_df.to_csv('submission.csv', index=False)
 sub_df