Kaggle competition: What's Cooking?
data = data_il.databunch().normalize(imagenet_stats )<define_variables>
with open('//kaggle/input/whats-cooking/train.json', 'r')as myfile:
train_data=myfile.read()
with open('//kaggle/input/whats-cooking/test.json', 'r')as myfile:
test_data=myfile.read()
train_obj = json.loads(train_data)
 test_obj = json.loads(test_data )
data.show_batch()<choose_model_class>
ingredients=[]
categories=[]
recipeCount=0
for recipe in train_obj:
recipeCount+=1
category=recipe['cuisine']
ingredientList=recipe['ingredients']
if category not in categories:
categories.append(category)
 for ingredient in ingredientList:
if ingredient not in ingredients:
ingredients.append(ingredient )
learn = cnn_learner(data, models.resnet50, model_dir='/temp/model',metrics=[accuracy] ).to_fp16()<find_best_params>
print(recipeCount,'recipes, ',len(categories),'categories, ', len(ingredients),' ingredients' )
learn.freeze()
learn.lr_find()<train_model>
xtrain=np.zeros(( recipeCount,len(ingredients)))
 ytrain=np.empty(recipeCount,dtype=object)
 for i in range(recipeCount):
ytrain[i]=train_obj[i]['cuisine']
for ingredient in train_obj[i]['ingredients']:
index=ingredients.index(ingredient)
 xtrain[i][index]=1
learn.fit_one_cycle(3, 1e-2 )<save_model>
l=len(test_obj)
 xtest=np.zeros(( l,len(ingredients)))
 testIndex=np.zeros(l,dtype=int)
 for i in range(l):
testIndex[i]=test_obj[i]['id']
for ingredient in test_obj[i]['ingredients']:
if ingredient in ingredients:
index=ingredients.index(ingredient)
 xtest[i][index]=1
learn.save(save_dir/'stage1-64-resnet50' )<load_pretrained>
lr=LogisticRegression(random_state=0, solver='sag',multi_class='auto')
 ypred=lr.fit(xtrain,ytrain ).predict(xtest )
learn.load(save_dir/'stage1-64-resnet50' )<find_best_params>
assert(testIndex.size==ypred.size)
 data = {'id':testIndex, 'cuisine':ypred}
submit = pd.DataFrame(data)
 submit.to_csv('submission.csv',index = None, header=True )