Kaggle competition: Bengali.AI Handwritten Grapheme Classification
<SOS> metric: WeightedCategorizationAccuracy Kaggle data source: bengali.ai-handwritten-grapheme-classification<install_modules>
import numpy as np
import pandas as pd
import os
!pip install -q efficientnet<set_options>
class DenseNetHead(nn.Module):
def __init__(self, in_features: int, n_classes: int):
super().__init__()
self.classifier = nn.Linear(in_features, n_classes)
 def forward(self, x):
return self.classifier(x)
 class DenseNetBase(nn.Module):
def __init__(self, name: str, pretrained: bool = True):
super().__init__()
self.base = getattr(models, name )(pretrained=pretrained)
 if name.endswith('121'):
self.out_features = 1024
elif name.endswith('161'):
self.out_features = 2208
elif name.endswith('169'):
self.out_features = 1664
else:
self.out_features = 1920
def forward(self, x):
base = self.base
features = base.features(x)
 out = F.relu(features, inplace=True)
 out = F.adaptive_avg_pool2d(out,(1, 1))
out = torch.flatten(out, 1)
 return out
%matplotlib inline
print("Tensorflow version " + tf.__version__ )<define_variables>
class ResNetHead(nn.Module):
def __init__(self, in_features: int, n_classes: int):
super().__init__()
self.pooling = nn.AdaptiveAvgPool2d(( 1, 1))
self.fc1 = nn.Linear(in_features, n_classes)
 def forward(self, x):
x = self.pooling(x)
 x = torch.flatten(x, start_dim=1)
 x = self.apply_fc_out(x)
 return x
def apply_fc_out(self, x):
return self.fc1(x)
 class ResNetBase(nn.Module):
def __init__(self, name: str, pretrained: bool, frozen_start: bool = False):
super().__init__()
self.base = getattr(models, name )(pretrained=pretrained)
 self.frozen_start = frozen_start
if name == 'resnet34' or name == 'resnet18':
self.out_features = 512
else:
self.out_features = 2048
self.frozen = []
if self.frozen_start:
self.frozen = [self.base.layer1, self.base.conv1, self.base.bn1]
for m in self.frozen:
self._freeze(m)
 def forward(self, x):
base = self.base
x = base.conv1(x)
 x = base.bn1(x)
 x = base.relu(x)
 x = base.maxpool(x)
 x = base.layer1(x)
 x = base.layer2(x)
 x = base.layer3(x)
 x = base.layer4(x)
 return x
def train(self, mode=True):
super().train(mode=mode)
 for m in self.frozen:
self._bn_to_eval(m)
 def _freeze(self, module):
for p in module.parameters() :
p.requires_grad = False
def _bn_to_eval(self, module):
for m in module.modules() :
if isinstance(m, nn.BatchNorm2d):
m.eval()
GCS_DS_PATH = KaggleDatasets().get_gcs_path("tpu-getting-started" )<define_variables>

 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 class TPS_SpatialTransformerNetwork(nn.Module):

 def __init__(self, F, I_size, I_r_size, I_channel_num=1):

 super(TPS_SpatialTransformerNetwork, self ).__init__()
self.F = F
self.I_size = I_size
self.I_r_size = I_r_size
self.I_channel_num = I_channel_num
self.LocalizationNetwork = LocalizationNetwork(self.F, self.I_channel_num)
 self.GridGenerator = GridGenerator(self.F, self.I_r_size)
 def forward(self, batch_I):
batch_C_prime = self.LocalizationNetwork(batch_I)
 build_P_prime = self.GridGenerator.build_P_prime(
 batch_C_prime)
 build_P_prime_reshape = build_P_prime.reshape(
 [build_P_prime.size(0), self.I_r_size[0], self.I_r_size[1], 2])
 if torch.__version__ > "1.2.0":
batch_I_r = F.grid_sample(batch_I, build_P_prime_reshape,
padding_mode='border', align_corners=True)
 else:
batch_I_r = F.grid_sample(batch_I, build_P_prime_reshape,
padding_mode='border')
 return batch_I_r
class LocalizationNetwork(nn.Module):

 def __init__(self, F, I_channel_num):
super(LocalizationNetwork, self ).__init__()
self.F = F
self.I_channel_num = I_channel_num
self.conv = nn.Sequential(
 nn.Conv2d(in_channels=self.I_channel_num, out_channels=64, kernel_size=3,
stride=1, padding=1,
bias=False), nn.BatchNorm2d(64), nn.ReLU(True),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 128, 3, 1, 1, bias=False), nn.BatchNorm2d(128), nn.ReLU(True),
nn.MaxPool2d(2, 2),
nn.Conv2d(128, 256, 3, 1, 1, bias=False), nn.BatchNorm2d(256), nn.ReLU(True),
nn.MaxPool2d(2, 2),
nn.Conv2d(256, 512, 3, 1, 1, bias=False), nn.BatchNorm2d(512), nn.ReLU(True),
nn.AdaptiveAvgPool2d(1)
)
 self.localization_fc1 = nn.Sequential(nn.Linear(512, 256), nn.ReLU(True))
self.localization_fc2 = nn.Linear(256, self.F * 2)
 self.localization_fc2.weight.data.fill_(0)

ctrl_pts_x = np.linspace(-1.0, 1.0, int(F / 2))
ctrl_pts_y_top = np.linspace(0.0, -1.0, num=int(F / 2))
ctrl_pts_y_bottom = np.linspace(1.0, 0.0, num=int(F / 2))
ctrl_pts_top = np.stack([ctrl_pts_x, ctrl_pts_y_top], axis=1)
 ctrl_pts_bottom = np.stack([ctrl_pts_x, ctrl_pts_y_bottom], axis=1)
 initial_bias = np.concatenate([ctrl_pts_top, ctrl_pts_bottom], axis=0)
 self.localization_fc2.bias.data = torch.from_numpy(initial_bias ).float().view(-1)
 def forward(self, batch_I):

 batch_size = batch_I.size(0)
 features = self.conv(batch_I ).view(batch_size, -1)
 batch_C_prime = self.localization_fc2(self.localization_fc1(features)).view(
 batch_size, self.F, 2)
 return batch_C_prime
class GridGenerator(nn.Module):

 def __init__(self, F, I_r_size):

 super(GridGenerator, self ).__init__()
self.eps = 1e-6
self.I_r_height, self.I_r_width = I_r_size
self.F = F
self.C = self._build_C(self.F)
 self.P = self._build_P(self.I_r_width, self.I_r_height)
 self.register_buffer("inv_delta_C", torch.tensor(
 self._build_inv_delta_C(self.F, self.C)).float())
 self.register_buffer("P_hat", torch.tensor(
 self._build_P_hat(self.F, self.C, self.P)).float())
 def _build_C(self, F):

 ctrl_pts_x = np.linspace(-1.0, 1.0, int(F / 2))
ctrl_pts_y_top = -1 * np.ones(int(F / 2))
ctrl_pts_y_bottom = np.ones(int(F / 2))
ctrl_pts_top = np.stack([ctrl_pts_x, ctrl_pts_y_top], axis=1)
 ctrl_pts_bottom = np.stack([ctrl_pts_x, ctrl_pts_y_bottom], axis=1)
 C = np.concatenate([ctrl_pts_top, ctrl_pts_bottom], axis=0)
 return C
def _build_inv_delta_C(self, F, C):

 hat_C = np.zeros(( F, F), dtype=float)
 for i in range(0, F):
for j in range(i, F):
r = np.linalg.norm(C[i] - C[j])
 hat_C[i, j] = r
hat_C[j, i] = r
np.fill_diagonal(hat_C, 1)
 hat_C =(hat_C ** 2)* np.log(hat_C)
 delta_C = np.concatenate(
 [
np.concatenate([np.ones(( F, 1)) , C, hat_C], axis=1),
np.concatenate([np.zeros(( 2, 3)) , np.transpose(C)], axis=1),
np.concatenate([np.zeros(( 1, 3)) , np.ones(( 1, F)) ], axis=1)
 ],
axis=0 
)
 inv_delta_C = np.linalg.inv(delta_C)
 return inv_delta_C
def _build_P(self, I_r_width, I_r_height):
I_r_grid_x =(np.arange(-I_r_width, I_r_width,
2)+ 1.0)/ I_r_width
I_r_grid_y =(np.arange(-I_r_height, I_r_height,
2)+ 1.0)/ I_r_height
P = np.stack(
 np.meshgrid(I_r_grid_x, I_r_grid_y),
axis=2 
)
 return P.reshape([-1, 2])
 def _build_P_hat(self, F, C, P):
n = P.shape[0]
P_tile = np.tile(np.expand_dims(P, axis=1), 
(1, F, 1))
C_tile = np.expand_dims(C, axis=0)
 P_diff = P_tile - C_tile
rbf_norm = np.linalg.norm(P_diff, ord=2, axis=2, keepdims=False)
 rbf = np.multiply(np.square(rbf_norm), np.log(rbf_norm + self.eps))
P_hat = np.concatenate([np.ones(( n, 1)) , P, rbf], axis=1)
 return P_hat
def build_P_prime(self, batch_C_prime):

 batch_size = batch_C_prime.size(0)
 batch_inv_delta_C = self.inv_delta_C.repeat(batch_size, 1, 1)
 batch_P_hat = self.P_hat.repeat(batch_size, 1, 1)
 batch_C_prime_with_zeros = torch.cat(( batch_C_prime, torch.zeros(
 batch_size, 3, 2 ).float().to(device)) , dim=1)
 batch_T = torch.bmm(batch_inv_delta_C,
batch_C_prime_with_zeros)
 batch_P_prime = torch.bmm(batch_P_hat, batch_T)
 return batch_P_prime
IMAGE_SIZE = [512, 512]
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 SEED = 2020<data_type_conversions>
def build_grapheme_model(base: str, n_classes: int, **kwargs)-> nn.Module:
return GraphemeModel(base=base, n_classes=n_classes, **kwargs)
 class GraphemeModel(nn.Module):
def __init__(self, *, base: str, n_classes: int, **base_kwargs,):
super().__init__()
if base.startswith('resne'):
self.base = ResNetBase(base, **base_kwargs)
 self.in_features = self.base.out_features
self.head = ResNetHead(
 in_features=self.in_features,
n_classes=n_classes, 
)
 elif base.startswith('vgg'):
self.base = VGGBase(base, **base_kwargs)
 self.head = VGGHead(
 n_classes=n_classes, 
)
 else:
self.base = DenseNetBase(base, **base_kwargs)
 self.in_features = self.base.out_features
self.head = DenseNetHead(
 in_features=self.in_features,
n_classes=n_classes, 
)
 def forward(self, x):
x = self.base(x)
 x = self.head(x)
 return x
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):

 ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_validation_dataset() :
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=True)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} validation images, {} unlabeled test images'.format(NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<choose_model_class>
def build_vc_model(base: str, n_classes: List[int], **kwargs)-> nn.Module:
return VCModel(base=base, n_classes=n_classes, **kwargs)
 class VCModel(nn.Module):
def __init__(self, *, base: str, n_classes: List[int], **base_kwargs,):
super().__init__()
self.transformation = TPS_SpatialTransformerNetwork(
 F=20,
I_size=(224, 224),
I_r_size=(224, 224),
I_channel_num=3, 
)
 if base.startswith('resne'):
self.base = ResNetBase(base, **base_kwargs)
 self.in_features = self.base.out_features
self.vowel_head = ResNetHead(
 in_features=self.in_features,
n_classes=n_classes[0], 
)
 self.consonant_head = ResNetHead(
 in_features=self.in_features,
n_classes=n_classes[1], 
)
 elif base.startswith('vgg'):
self.base = VGGBase(base, **base_kwargs)
 self.vowel_head = VGGHead(
 n_classes=n_classes[0], 
)
 self.consonant_head = VGGHead(
 n_classes=n_classes[1], 
)
 else:
self.base = DenseNetBase(base, **base_kwargs)
 self.in_features = self.base.out_features
self.vowel_head = DenseNetHead(
 in_features=self.in_features,
n_classes=n_classes[0], 
)
 self.consonant_head = DenseNetHead(
 in_features=self.in_features,
n_classes=n_classes[1], 
)
 def forward(self, x):
x = self.transformation(x)
 x = self.base(x)
 vowel = self.vowel_head(x)
 consonant = self.consonant_head(x)
 return vowel, consonant
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model1 = get_model(DenseNet201)
 model1.load_weights("/kaggle/input/start-with-densenet201/my_densenet_201.h5" )<choose_model_class>
from pathlib import Path
import pandas as pd
import os
import torchvision
import torch
from torch.nn import Module
from PIL import Image
import cv2
from torch.utils.data import Dataset, DataLoader
import numpy as np
from tqdm import tqdm
from albumentations import Compose
from albumentations.pytorch import ToTensorV2
import albumentations as A
def get_model(use_model):
base_model = use_model(weights='noisy-student',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model2 = get_model(EfficientNetB7)
 model2.load_weights("/kaggle/input/start-with-pre-train/my_ef_net_b7.h5" )<predict_on_test>
import torch.nn.functional as F
val_dataset = get_validation_dataset()
images_ds = val_dataset.map(lambda image, label: image)
 labels_ds = val_dataset.map(lambda image, label: label ).unbatch()
val_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
m1 = model1.predict(images_ds)
 m2 = model2.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
val_probabilities = alpha*m1+(1-alpha)*m2
val_predictions = np.argmax(val_probabilities, axis=-1)
 scores.append(f1_score(val_labels, val_predictions, labels=range(104), average='macro'))
best_alpha = np.argmax(scores)/100
print('Best alpha: ' + str(best_alpha))<predict_on_test>
BASE = 'resnext50_32x4d'
HEAD_DROPOUT = 0.5
FROZEN_START = 0
CLASSES = [168, 11, 7]
FP16 = True
HEAD = 'SimpleHead'
test_ds = get_test_dataset(ordered=True)
 print('Вычисляем предсказания...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities1 = model1.predict(test_images_ds)
 probabilities2 = model2.predict(test_images_ds)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Создание файла submission.csv...')
 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='' )<set_options>
GRAPHEME_BASE = 'resnext50_32x4d'
VC_BASE = 'resnext50_32x4d'
GRAPHEME_CLASSES = 168
VC_CLASSES = [11, 7]
SIZE=224
HEIGHT = 137
WIDTH = 236
GRAPHEME_WEIGHTS_FILE = os.path.abspath('/kaggle/input/vcmodels/model_best_g_0.pth')
 VC_WEIGHTS_FILE = os.path.abspath('/kaggle/input/vcmodels/stn_resnext50/model_best_1.pth' )
%matplotlib inline
print("Tensorflow version " + tf.__version__ )<define_variables>
class GraphemeDataset(Dataset):
def __init__(self, fname, transform=None):
self.df = pd.read_parquet(fname)
 self.data = self.df.iloc[:, 1:].values
self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
name = self.df.iloc[idx,0]
img = 255 - self.data[idx, :].reshape(HEIGHT, WIDTH ).astype(np.uint8)
 img =(img*(255.0/img.max())).astype(np.uint8)
 img = crop_resize(img)
 if self.transform:
transformed = self.transform(image=img)
 img = transformed['image']
return img, name
GCS_DS_PATH = KaggleDatasets().get_gcs_path("tpu-getting-started" )<define_variables>
TEST = ['/kaggle/input/bengaliai-cv19/test_image_data_0.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_1.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_2.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_3.parquet']
IMAGE_SIZE = [512, 512]
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 SEED = 2020<data_type_conversions>
transform_test = Compose([
A.Normalize(mean=(0.0692, 0.0692, 0.0692),
std=(0.2052, 0.2052, 0.2052)) ,
ToTensorV2()
] )
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):

 ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_validation_dataset() :
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=True)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} validation images, {} unlabeled test images'.format(NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<choose_model_class>
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 grapheme_model: Module = build_grapheme_model(
 base=GRAPHEME_BASE,
pretrained=False,
n_classes=GRAPHEME_CLASSES, 
)
 print('Creating model...')
 grapheme_model.load_state_dict(torch.load(GRAPHEME_WEIGHTS_FILE, map_location=device))
grapheme_model = grapheme_model.to(device)
 grapheme_model.eval()
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model1 = get_model(DenseNet201)
 model1.load_weights("/kaggle/input/start-with-densenet201/my_densenet_201.h5" )<choose_model_class>
vc_model: Module = build_vc_model(
 base=VC_BASE,
pretrained=False,
n_classes=VC_CLASSES, 
)
 print('Creating model...')
 vc_model.load_state_dict(torch.load(VC_WEIGHTS_FILE, map_location=device))
vc_model = vc_model.to(device)
 vc_model.eval()
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model2 = get_model(ResNet101V2)
 model2.load_weights("/kaggle/input/start-with-pre-train/my_ef_net_b7.h5" )<predict_on_test>
row_id,target = [],[]
for fname in TEST:
ds = GraphemeDataset(fname, transform=transform_test)
 dl = DataLoader(ds, batch_size=256, shuffle=False)
 with torch.no_grad() :
for x, y in tqdm(dl):
x = x.to(device, dtype=torch.float)
 pred_g = grapheme_model(x)
 pred_v, pred_c = vc_model(x)
 pred_g = F.softmax(pred_g, dim=1 ).data.cpu().numpy().argmax(axis=1)
 pred_v = F.softmax(pred_v, dim=1 ).data.cpu().numpy().argmax(axis=1)
 pred_c = F.softmax(pred_c, dim=1 ).data.cpu().numpy().argmax(axis=1)
 for idx,name in enumerate(y):
row_id += [f'{name}_grapheme_root',f'{name}_vowel_diacritic',
f'{name}_consonant_diacritic']
target += [pred_g[idx].item() ,pred_v[idx].item() ,pred_c[idx].item() ]
<predict_on_test><EOS> 
sub_df = pd.DataFrame({'row_id': row_id, 'target': target})
 sub_df.to_csv('submission.csv', index=False)
 sub_df.head()