Kaggle competition: Bengali.AI Handwritten Grapheme Classification
evaluate_results(test, submission )<import_modules>
IMAGE_SIZE = 137, 236
class DatasetRetriever(Dataset):
def __init__(self, df):
self.image_ids = df.iloc[:, 0].tolist()
self.images = torch.from_numpy(255 - df[[str(x)for x in range(32332)]].values)
 def __len__(self):
return self.images.shape[0]
def __getitem__(self, idx):
img = self.images[idx]
img = img.view(*IMAGE_SIZE)
 img = img.to(torch.float32)/ 255.
 img = img.unsqueeze(0)
 img = img.repeat(3, 1, 1)
 img_id = self.image_ids[idx]
return img_id, img
class Predictor:
def __init__(self, model):
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 self.model = model.to(self.device, dtype=torch.float32);
self.model.eval()
print(f'Model prepared.Device is {self.device}')
 def predict(self, inputs):
inputs = inputs.to(self.device, dtype=torch.float32)
 with torch.no_grad() :
outputs = self.model(inputs)
 return outputs
def load(self, path):
checkpoint = torch.load(path, map_location=self.device)
 self.model.load_state_dict(checkpoint['model_state_dict'] )
import itertools
from tqdm import tqdm
from math import ceil, sqrt
from typing import List, Dict, Set, Optional, Tuple, Union
from collections import defaultdict, Counter<import_modules>
predictors = []
for i,(model_path, net, coef)in enumerate([ 
('/kaggle/input/ghostnetbengali/checkpoint.pt', ghost_net(num_classes=168+11+7), 0.5), 
('/kaggle/input/ghostnetbengali/densenet121-checkpoint.pt', densenet121(num_classes=168+11+7), 0.5),
]):
predictor = Predictor(net)
 predictor.load(model_path)
 predictors.append(( predictor, coef))
def predict_to_numpy(predict):
return torch.nn.functional.softmax(predict, dim=1 ).data.cpu().numpy().argmax(axis=1)
 def make_prediction(images):
outputs = 0
for predictor, coef in predictors:
outputs += predictor.predict(images)* coef
roots = predict_to_numpy(outputs[:,:168])
 vowels = predict_to_numpy(outputs[:,168:168+11])
 consonants = predict_to_numpy(outputs[:,168+11:])
 return roots, vowels, consonants
class Point:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __repr__(self):
return f"Point({self.x}, {self.y})"
def distance_from(self, other: "Point")-> int:
return ceil(sqrt(( self.x - other.x)** 2 +(self.y - other.y)** 2))
class _Obj:
def __init__(self, id: int):
self._id = id
def __repr__(self):
return f"{self.__class__.__name__} {self._id}"
@property
def id(self):
return self._id
class Product(_Obj):
def __init__(self, id: int, weight: int):
super().__init__(id)
 self._weight = weight
@property
def weight(self):
return self._weight
class _PositionObj(_Obj):
def __init__(self, id: int, position: Point):
super().__init__(id)
 self._position = position
def __repr__(self):
return f"{self.__class__.__name__} {self._id} at {self._position}"
@property
def position(self):
return self._position
def distance_from(self, _other: "_PositionObj")-> int:
return self._position.distance_from(_other.position)
 def find_nearest_object(
 self, _others: List["_PositionObj"] 
)-> Tuple["_PositionObj", int]:
nearest = _others[0]
distance = self.distance_from(nearest)
 for other in _others[1:]:
d = self.distance_from(other)
 if d < distance:
nearest, distance = other, d
return nearest, distance
class _ProductHolder(_PositionObj):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
 self._products: Dict[Product, int] = {}
@property
def products(self):
return self._products
def add_products(self, products: Dict[Product, int]):
for product, quantity in products.items() :
if product in self._products:
self._products[product] += quantity
else:
self._products[product] = quantity
def remove_products(self, products: Dict[Product, int]):
for product, quantity in products.items() :
self._products[product] -= quantity
if self._products[product] == 0:
self._products.pop(product)
 @property
def product_weight(self):
return sum(p.weight * q for p, q in self._products.items())
 def remove_all_products(self):
self._products = {}
class Order(_ProductHolder):
def __init__(self, id: int, position: Point, products: Dict[Product, int]):
super().__init__(id, position)
 self._products = products
def is_complete(self):
return not self._products
class Drone(_ProductHolder):
def __init__(self, id: int, position: Point, max_capacity: int):
super().__init__(id, position)
 self.max_capacity = max_capacity
self.is_busy = False
self.stops_being_busy_at = 0
def set_position(self, position: Point):
self._position = position
def update_state(self, current_time):
if self.is_busy and current_time == self.stops_being_busy_at:
self.is_busy = False
@property
def current_capacity(self)-> int:
return self.max_capacity - self.product_weight
def get_products_that_can_fit_out_of(
 self, products: Dict[Product, int] 
)-> Dict[Product, int]:
can_carry = defaultdict(int)
 products = sorted(
 [(p, q)for p, q in products.items() ], key=lambda x: -x[0].weight 
)
 capacity = self.current_capacity
for product, quantity in products:
if not quantity:
continue
weight = product.weight
if weight > capacity:
continue
for _ in range(quantity):
can_carry[product] += 1
capacity -= weight
if weight > capacity:
break
return can_carry
class Warehouse(_ProductHolder):
def __init__(self, id: int, position: Point, products=Dict[Product, int]):
super().__init__(id, position)
 self._products = products
def get_products_that_are_in_stock_out_of(
 self, products=Dict[Product, int] 
)-> Dict[Product, int]:
return {p: min(q, self._products.get(p, 0)) for p, q in products.items() }
class Command:
def __init__(
 self,
drone: Drone,
destination: Union[Warehouse, Order],
product: Product,
quantity: int, 
):
self.drone = drone
self.destination = destination
self.product = product
self.quantity = quantity
def __repr__(self):
return f"Command({self.drone} -> {self.destination}: {self.product} * {self.quantity})"
def to_string(self):
if isinstance(self.destination, Warehouse):
str_command = "L"
elif isinstance(self.destination, Order):
str_command = "D"
else:
raise ValueError("Destination must be a warehouse or order.")
 return f"{self.drone.id} {str_command} {self.destination.id} {self.product.id} {self.quantity}"<load_from_csv>
%%time
target = []
row_id = []
for i in range(4):
data = pd.read_parquet(f'.. /input/bengaliai-cv19/test_image_data_{i}.parquet')
 dataset = DatasetRetriever(data)
 data_loader = DataLoader(dataset, batch_size=256, num_workers=4, shuffle=False, sampler=SequentialSampler(dataset))
for idx,(image_ids, images)in tqdm(enumerate(data_loader), total=len(data_loader)) :
roots, vowels, consonants = make_prediction(images)
 for image_id, root, vowel, consonant in zip(image_ids, roots, vowels, consonants):
row_id.append(image_id + '_consonant_diacritic')
 target.append(consonant)
 row_id.append(image_id + '_grapheme_root')
 target.append(root)
 row_id.append(image_id + '_vowel_diacritic')
 target.append(vowel )
<categorify><EOS> 
df_submission = pd.DataFrame(
 {
'row_id': row_id,
'target': target
},
columns=['row_id','target'] 
)
 df_submission.to_csv('submission.csv', index=False)
 df_submission.head(10 )