Kaggle competition: ISSM2020 AI Challenge
<SOS> metric: categorizationaccuracy Kaggle data source: issm2020-ai-challenge<define_variables>
sys.path.insert(0, '/kaggle/input/efficientnetmodel/efficientnetModel.py')
 sys.path.append('/kaggle/input/efficientnetmodel')
 sys.path.append('/kaggle/input/efficientnetmodel/utils.py')
category_colums=[colum for colum in X.columns if X[colum].dtype == 'object']
len(category_colums )<count_missing_values>

 IMAGE_SIZE = 320
def int_parameter(level, maxval):

 return int(level * maxval / 10)
 def float_parameter(level, maxval):

 return float(level)* maxval / 10.
 def sample_level(n):
return np.random.uniform(low=0.1, high=n)
 def autocontrast(pil_img, _):
return ImageOps.autocontrast(pil_img)
 def equalize(pil_img, _):
return ImageOps.equalize(pil_img)
 def posterize(pil_img, level):
level = int_parameter(sample_level(level), 4)
 return ImageOps.posterize(pil_img, 4 - level)
 def rotate(pil_img, level):
degrees = int_parameter(sample_level(level), 30)
 if np.random.uniform() > 0.5:
degrees = -degrees
return pil_img.rotate(degrees, resample=Image.BILINEAR)
 def solarize(pil_img, level):
level = int_parameter(sample_level(level), 256)
 return ImageOps.solarize(pil_img, 256 - level)
 def shear_x(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
 if np.random.uniform() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, level, 0, 0, 1, 0),
resample=Image.BILINEAR)
 def shear_y(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
 if np.random.uniform() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, 0, 0, level, 1, 0),
resample=Image.BILINEAR)
 def translate_x(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
 if np.random.random() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, 0, level, 0, 1, 0),
resample=Image.BILINEAR)
 def translate_y(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
 if np.random.random() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, 0, 0, 0, 1, level),
resample=Image.BILINEAR)
 def color(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Color(pil_img ).enhance(level)
 def contrast(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Contrast(pil_img ).enhance(level)
 def brightness(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Brightness(pil_img ).enhance(level)
 def sharpness(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Sharpness(pil_img ).enhance(level)
 augmentations = [
autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
translate_x, translate_y
]
augmentations_all = [
autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
translate_x, translate_y, color, contrast, brightness, sharpness
]
y.isna().sum()<count_missing_values>
use_gpu = torch.cuda.is_available()
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
dir_test = ".. /input/issm2020-ai-challenge/semTest/semTest/"
data_dir = '.. /input/issm2020-ai-challenge/semTrain/semTrain'
batch_size = 16
lr = 0.01
momentum = 0.9
num_epochs = 100
input_size = 224
class_num = 10
net_name = 'efficientnet-oriDuAug-b7'
mixture_width = 3
mixture_depth = -1
aug_severity = 3
all_ops = True
no_jsd = True
def aug(image, preprocess):

 aug_list = augmentations
if all_ops:
aug_list = augmentations_all
ws = np.float32(np.random.dirichlet([1] * mixture_width))
m = np.float32(np.random.beta(1, 1))
mix = torch.zeros_like(preprocess(image))
for i in range(mixture_width):
image_aug = image.copy()
depth = mixture_depth if mixture_depth > 0 else np.random.randint(
 1, 4)
 for _ in range(depth):
op = np.random.choice(aug_list)
 image_aug = op(image_aug, aug_severity)
 mix += ws[i] * preprocess(image_aug)
 mixed =(1 - m)* preprocess(image)+ m * mix
return mixed
class AugMixDataset(torch.utils.data.Dataset):

 def __init__(self, dataset, preprocess, no_jsd=False):
self.dataset = dataset
self.preprocess = preprocess
self.no_jsd = no_jsd
def __getitem__(self, i):
x, y = self.dataset[i]
if self.no_jsd:
return aug(x, self.preprocess), y
else:
im_tuple =(self.preprocess(x), aug(x, self.preprocess),
aug(x, self.preprocess))
return im_tuple, y
def __len__(self):
return len(self.dataset)
 augmix_tf: Compose = transforms.Compose(
 [
transforms.Resize(input_size),
transforms.RandomHorizontalFlip() ,
transforms.RandomCrop(input_size, padding=16)
 ] 
)
 preprocess = transforms.Compose(
 [
transforms.Resize(input_size),
transforms.ToTensor() ,
transforms.Normalize(( 0.4914, 0.4822, 0.4465),(0.2023, 0.1994, 0.2010))
])
 def loaddata(data_dir, batch_size, set_name, shuffle):
data_transforms = {
'train': transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.RandomAffine(degrees=0, translate=(0.05, 0.05)) ,
transforms.RandomHorizontalFlip() ,
transforms.RandomGrayscale(p=0.1),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ]),
'test': transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ]),
}
image_datasets = datasets.ImageFolder(data_dir, augmix_tf)
 image_datasets = AugMixDataset(image_datasets, preprocess, no_jsd)
 dataset_loaders = torch.utils.data.DataLoader(image_datasets,
batch_size=batch_size,
shuffle=shuffle, num_workers=0)
 data_set_sizes = len(image_datasets)
 return dataset_loaders, data_set_sizes
def train_model(model_ft, criterion, optimizer, lr_scheduler, num_epochs):
train_loss = []
since = time.time()
best_model_wts = model_ft.state_dict()
best_acc = 0.0
model_ft.train(True)
 for epoch in range(num_epochs):
dset_loaders, dset_sizes = loaddata(data_dir=data_dir, batch_size=batch_size, set_name='train', shuffle=True)
 print('Data Size', dset_sizes)
 print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
 optimizer = lr_scheduler(optimizer, epoch)
 running_loss = 0.0
running_corrects = 0
count = 0
for data in dset_loaders:
if count==189:
break
inputs, labels = data
labels = torch.squeeze(labels.type(torch.LongTensor))
if use_gpu:
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
 else:
inputs, labels = Variable(inputs), Variable(labels)
 outputs = model_ft(inputs)
 loss = criterion(outputs, labels)
 _, preds = torch.max(outputs.data, 1)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
count += 1
if count % 10 == 0 or outputs.size() [0] < batch_size:
print('Epoch:{}: loss:{:.5f}'.format(epoch, loss.item()))
train_loss.append(loss.item())
 running_loss += loss.item() * inputs.size(0)
 running_corrects += torch.sum(preds == labels.data)
 epoch_loss = running_loss / dset_sizes
epoch_acc = running_corrects.double() / dset_sizes
print('Loss: {:.4f} Acc: {:.6f}'.format(
 epoch_loss, epoch_acc))
model_ft.load_state_dict(best_model_wts)
 model_out_path = model_dir + "/" + net_name + '{}.pth'.format(epoch)
 print(model_dir + '/' + net_name + '{}.pth'.format(epoch))
torch.save(model_ft, model_out_path)
 if epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = model_ft.state_dict()
if epoch_acc > 0.999:
break
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
 time_elapsed // 60, time_elapsed % 60))
return train_loss, best_model_wts
def test_csv(model):
transform_test = transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 results = []
image_number = 0
resultFrame = pd.DataFrame(columns=["Id","LABEL"])
 img_test = os.listdir(dir_test)
 img_test.sort()
for i in range(len(img_test)) :
img = Image.open(dir_test + img_test[i])
 input = transform_test(img ).cuda()
input = input.unsqueeze(0)
 input = Variable(input)
 score = model(input)
 probability = torch.nn.functional.softmax(score, dim=1)
 max_value, index = torch.max(probability, 1)
 image_number += 1
probability = np.round(probability.cpu().detach().numpy())
 index = np.round(index.cpu().detach().numpy())
 resultFrame = resultFrame.append({'Id':int(image_number),'LABEL': int(index[0] + 1)}, ignore_index=True)
 print(resultFrame)
 resultFrame.to_csv('submission.csv', index=False)
 def exp_lr_scheduler(optimizer, epoch, init_lr=lr, lr_decay_epoch=10):
X_train.isna().sum()<count_missing_values>
model_ft=torch.load('/kaggle/input/efficientnetmodel/efficientnet-oriDuAug-b799.pth')
 model_ft = model_ft.eval().cuda()
test_csv(model_ft )