Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
!pip install efficientnet tensorflow_addons > /dev/null
if torch.cuda.is_available() :
map_location=lambda storage, loc: storage.cuda()
else:
map_location='cpu'<set_options>
%matplotlib inline
if torch.cuda.is_available() :
device= 'cuda'
else:
device='cpu'
print(device )<categorify>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
def get_transforms() :
return Compose([
Resize(IMAGE_SIZE, IMAGE_SIZE),
Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensorV2()
] )<choose_model_class>
X_train = X_train / 255.0
test = test / 255.0
class ResNet200D(nn.Module):
def __init__(self, model_name='resnet200d'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output<categorify>
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(y_preds1.sigmoid().to('cpu' ).numpy() + y_preds2.sigmoid().to('cpu' ).numpy())/ 2
avg_preds.append(y_preds)
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs<define_variables>
Y_train = tf.keras.utils.to_categorical(Y_train, num_classes = 10 )
MODEL_PATH = '.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold4_cv954.pth'
MODEL_PATH957 = '.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold3_cv957.pth'<choose_model_class>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=0 )
model = ResNet200D()
model.load_state_dict(torch.load(MODEL_PATH,map_location=map_location),strict=False)
 model.eval()
models = [model.to(device)]
model957 = ResNet200D()
model957.load_state_dict(torch.load(MODEL_PATH957,map_location=map_location),strict=False)
 model957.eval()
models957 = [model957.to(device)]<load_from_csv>
efficientnet_size = 7
weights = "imagenet"
size = 56
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 test_dataset = TestDataset(test, transform=get_transforms())
 test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False,
num_workers=4 , pin_memory=True)
 predictions = inference(models, test_loader, device)
 predictions957 = inference(models957, test_loader, device)
<save_to_csv>
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters = 3, kernel_size = 1, padding = 'Same',
activation ='relu', input_shape =(size,size,1)))
 model.add(getattr(efn, f"EfficientNetB{efficientnet_size}" )(
 weights=weights, include_top=False, input_shape=(size, size, 3)))
 model.add(tf.keras.layers.GlobalAveragePooling2D())
 model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dense(10, activation = "softmax"))
target_cols = test.iloc[:, 1:12].columns.tolist()
test[target_cols] =(predictions + predictions957)/2
test[['StudyInstanceUID'] + target_cols].to_csv('submission.csv', index=False)
 test.head()<import_modules>
optimizer = tf.keras.optimizers.Adam(lr=0.0001 )
<define_variables>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
AUTO = tf.data.experimental.AUTOTUNE
DIM = 600
IMAGE_SIZE=[DIM,DIM]
BATCH_SIZE = 8
DATA_PATH = ".. /input/ranzcr-clip-catheter-line-classification/"
OUTPUT_PATH = "./"<install_modules>
tf.random.set_seed(0)
 history = model.fit(X_train,
Y_train,
epochs=10,
batch_size=128,
validation_data=(X_val, Y_val),
)
!pip install.. /input/kerasapplications/keras-applications-master/
package_path = '.. /input/efficientnetmaster/efficientnet-master/'
sys.path.append(package_path)
<define_variables>
pred = model.predict_classes(test )
TEST_FILENAMES = tf.io.gfile.glob('.. /input/ranzcr-clip-catheter-line-classification/test_tfrecords/*.tfrec')
 print(TEST_FILENAMES )<categorify>
submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv" )
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, [DIM, DIM])
 image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
'image': tf.io.FixedLenFeature([], tf.string),
'StudyInstanceUID': tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['StudyInstanceUID']
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_unlabeled_tfrecord, num_parallel_calls = 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_TEST_IMAGES = count_data_items(TEST_FILENAMES )<load_pretrained>
submission["Label"] = pred
models1=[]
for filename in glob.glob('.. /input/eff7trained/*best.h5'):
model = tf.keras.models.load_model(filename, custom_objects = None)
 models1.append(model)
<predict_on_test>
submission.to_csv("submission.csv",index=False )