Kaggle competition: APTOS 2019 Blindness Detection
def read_dataset(path):
return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json' )<string_transform>
from fastai.vision import *
from torchvision import transforms
from fastai.vision.image import *
import torchvision
def generate_text(data):
text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
return text_data<define_variables>
Df = pd.read_csv(".. /input/aptos2019-blindness-detection/train.csv" )
train_text = generate_text(train)
 test_text = generate_text(test)
 target = [doc['cuisine'] for doc in train]<feature_engineering>
dev = torch.device("cuda:0")
print("TF-IDF on text data...")
 tfidf = TfidfVectorizer(binary=True)
 def tfidf_features(txt, flag):
if flag == "train":
x = tfidf.fit_transform(txt)
 else:
x = tfidf.transform(txt)
 x = x.astype('float16')
 return x<prepare_x_and_y>
data_bunch =(ImageList.from_df(Df,".. /input/aptos2019-blindness-detection/train_images",cols = 0, suffix = '.png' ).
 split_none().
 label_from_df(1,label_cls = FloatList ).
 transform(get_transforms(do_flip = True, flip_vert = True), size = 512 ).
 databunch(bs = 8 ).
 normalize(imagenet_stats))
X = tfidf_features(train_text, flag="train")
 X_test = tfidf_features(test_text, flag="test" )<categorify>
model = torchvision.models.resnet101(pretrained=False)
 model.load_state_dict(torch.load(".. /input/pytorch-models/resnet101-5d3b4d8f.pth"))
num_features = model.fc.in_features
model.fc = nn.Linear(2048, 1)
 model = model.to(dev )
lb = LabelEncoder()
y = lb.fit_transform(target )<choose_model_class>
learn= Learner(data_bunch,model, loss_func = mse, metrics=mse, model_dir=".. /.. /.. /models" )
classifier = SVC(C=100,
kernel='rbf',
degree=3,
gamma=1,
coef0=1,
shrinking=True,
tol=0.001,
probability=False,
cache_size=200,
class_weight=None,
verbose=False,
max_iter=-1,
decision_function_shape=None,
random_state=None)
 model = OneVsRestClassifier(classifier, n_jobs=4)
 model.fit(X, y )<save_to_csv>
learn.fit_one_cycle(5,2.5e-4 )
print("Label Encode the Target Variable...")
 print("Train the model...")
 print("Predict on test data...")
 y_test = model.predict(X_test)
 y_pred = lb.inverse_transform(y_test)
 print("Generate Submission File...")
 test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('svm_output.csv', index=False )<import_modules>
learn.fit_one_cycle(5,2.5e-4 )
from sklearn.model_selection import GridSearchCV
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
import pandas as pd
import json<load_from_csv>
learn.fit_one_cycle(5,2.5e-5 )
print("Read Dataset...")
 def read_dataset(path):
	return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json')
 print("Prepare text data of Train and Test...")
 def generate_text(data):
	text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
	return text_data
train_text = generate_text(train)
 test_text = generate_text(test)
 target = [doc['cuisine'] for doc in train]
print("TF-IDF on text data...")
 tfidf = TfidfVectorizer(binary=True)
 def tfidf_features(txt, flag):
if flag == "train":
	x = tfidf.fit_transform(txt)
 else:
	 x = tfidf.transform(txt)
 x = x.astype('float16')
 return x
X = tfidf_features(train_text, flag="train")
 X_test = tfidf_features(test_text, flag="test")
 print("Label Encode the Target Variable...")
 lb = LabelEncoder()
y = lb.fit_transform(target )<train_model>
learn.fit_one_cycle(5,1e-6 )
print("Train the model...")
 parameters = {'kernel':('linear', 'rbf', 'poly'), 'C':[1, 100]}
classifier = SVC(degree=3,
	 			 gamma=1,
	 			 coef0=1,
	 			 shrinking=True,
	 			 tol=0.001,
	 		 probability=False,
	 		 cache_size=200,
	 		 class_weight=None,
	 		 verbose=False,
	 		 max_iter=-1,
		 decision_function_shape=None,
		 random_state=None)
 model = GridSearchCV(classifier, parameters)
 model.fit(X, y )<save_to_csv>
Tf = partial(Image.apply_tfms,tfms=get_transforms(do_flip=True, flip_vert = True)[0][1:]+get_transforms(do_flip=True, flip_vert = True)[1],size = 512 )
print("Predict on test data...")
 y_test = model.predict(X_test)
 y_pred = lb.inverse_transform(y_test)
 print("Generate Submission File...")
 test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('svm_output.csv', index=False )<import_modules>
sub = pd.read_csv(".. /input/aptos2019-blindness-detection/sample_submission.csv" )
<load_from_disk><EOS> 
for i in range(len(sub.id_code)) :
s = 0
img = open_image(".. /input/aptos2019-blindness-detection/test_images/"+sub.id_code[i]+'.png')
 for i in range(64):
Img = Tf(img)
 p = learn.predict(Img)
 s+=p[1]
s = s/64.0
if s<0.5:
sub.diagnosis[i]=0
elif s>=0.4 and s<1.4:
sub.diagnosis[i]=1
elif s>=1.4 and s <2.4:
sub.diagnosis[i]=2
elif s>=2.4 and s <3.4:
sub.diagnosis[i]=3
else:
sub.diagnosis[i]=4
sub.to_csv("submission.csv",index=False )