Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import preprocessing
from sklearn.model_selection import GridSearchCV,StratifiedKFold,cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier,IsolationForest, RandomForestClassifier, VotingClassifier
test_pred_df.to_csv('submission.csv', index=False )<import_modules>
sns.set(rc={'figure.figsize':(8,6)})
 sns.set_style("darkgrid" )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
<set_options>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv" )
CFG = {
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 512,
'valid_bs': 16,
'device': 'cuda' if torch.cuda.is_available() else 'cpu'
}<load_from_csv>
test_ids = test['PassengerId']
df = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv')
 df_torch = df.copy()
df_tf = df.copy() 
<define_variables>
rng = np.random.RandomState(42)
 clf = IsolationForest(max_samples=100, random_state=rng,contamination = 0.05)
 clf.fit(train[["Age","SibSp","Parch","Fare"]].dropna())
 prediction = clf.predict(train[["Age","SibSp","Parch","Fare"]].dropna())
 df_outliers = train[["Age","SibSp","Parch","Fare"]].dropna()
df_outliers['Outlier'] = prediction
index_list = df_outliers[df_outliers['Outlier'] == -1].index
train.drop(index_list, inplace=True )
PATH = '/kaggle/input/cassava-leaf-disease-classification/test_images/'<choose_model_class>
df = pd.concat(objs=[train, test], axis=0 ).reset_index(drop=True )
class CassavaImageClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<categorify>
df.isnull().sum()
class DiseaseDatasetInference(torch.utils.data.Dataset):
def __init__(self, df, transform=None, opt_label=True):
self.df = df.reset_index(drop=True ).copy()
self.transform = transform
self.opt_label = opt_label
if self.opt_label:
self.data = [(row['image_id'], row['label'])for _, row in self.df.iterrows() ]
else:
self.data = [(row['image_id'])for _, row in self.df.iterrows() ]
self.data = np.asarray(self.data)
 def __len__(self):
return len(self.data)
 def __getitem__(self, index):
if self.opt_label:
image_path, label = self.data[index]
else:
image_path = self.data[index]
image = cv2.imread(image_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform is not None:
image = self.transform(image=image)['image']
if self.opt_label == True:
return(image, int(label))
else:
return image<load_pretrained>
df["Fare"].isnull().sum()
trained_model = torch.load('/kaggle/input/cassava-effecientnet-b3/model_6.pt', map_location=torch.device(CFG['device']))<load_from_csv>
df["Fare"].fillna(df["Fare"].median() ,inplace=True )
test_csv = df.copy()
test_csv['image_id'] = PATH + test_csv['image_id']
test_ds = DiseaseDatasetInference(test_csv, transform=get_inference_transforms() , opt_label=False)
 test_loader = torch.utils.data.DataLoader(test_ds, batch_size=CFG['valid_bs'], shuffle=False, pin_memory=False )<concatenate>
df["Fare"] = np.log(df["Fare"] ).replace(-np.inf, 0 )
torch_outcomes = pd.concat([df_torch['image_id'], pd.DataFrame(preds_torch)], axis=1 ).sort_values(['image_id'] )<import_modules>
df["Embarked"].isnull().sum()
import tensorflow as tf
import pandas as pd
import numpy as np
import os
from PIL import Image<load_pretrained>
df["Embarked"] = df["Embarked"].fillna("S" )
model = tf.keras.models.load_model('/kaggle/input/plantdiseaseresnet50/resnet50.h5' )<concatenate>
df.isna().sum()
tf_outcomes = pd.concat([pd.DataFrame(test_images, columns=['image_id']), pd.DataFrame(preds_tf)], axis=1 ).sort_values(['image_id'] )<data_type_conversions>
df["Sex"] = df["Sex"].map({"male": 0, "female":1} )
final_preds =(torch_outcomes.drop('image_id', axis=1)*0.7 + tf_outcomes.drop('image_id', axis=1)*0.3 ).to_numpy().argmax(1 )<save_to_csv>
ages=[]
for i in range(1,4):
ages.append(((df[df['Pclass'] == i].Age ).median()))
for index, row in df.iterrows() :
if(np.isnan(row['Age'])) :
df.loc[index,'Age'] = ages[row['Pclass'] - 1]
submit = pd.DataFrame({'image_id': torch_outcomes['image_id'].values, 'label': final_preds})
 submit.to_csv('submission.csv', index=False )<install_modules>
df.isna().sum()
!pip install.. /input/easydict/easydict-1.9-py2.py3-none-any.whl 
<set_options>
df["Cabin"].isnull().sum()
sys.path.insert(1, '.. /input/snapmix/')
 def set_env(seed=0):
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 def predict(model,testloader,midlevel=False):
model.eval()
time_start = time.time()
pbar = tqdm(testloader, dynamic_ncols=True, total=len(testloader))
pres = []
for idx,(input, _)in enumerate(pbar):
input = input.cuda()
if conf.tta is None:
output,_,moutput = model(input)
 else:
bs, ncrops, c, h, w = input.size()
output,_,moutput = model(input.view(-1,c,h,w))
output = output.view(bs, ncrops, -1 ).mean(1)
 moutput = moutput.view(bs, ncrops, -1 ).mean(1)
 if midlevel:
foutput = output + moutput
else:
foutput = output
pre = torch.argmax(foutput,dim=1)
 pres.append(pre)
 pres = torch.cat(pres)
 return pres
def get_dataset(conf):
datadir = 'data/cassava'
if conf and 'datadir' in conf:
datadir = conf.datadir
trainpd,valpd = None,None
testimgdir = datadir + '/test_images'
testfile = glob.glob(testimgdir+'/*.jpg')
 testfile = [os.path.basename(fn)for fn in testfile]
testpd = pd.DataFrame(testfile, columns =['image_id'])
 testpd['label'] = 0
if 'foldid' in conf:
traindata = pd.read_csv(datadir+'/train.csv')
 folds = StratifiedKFold(n_splits=5 ).split(np.arange(traindata.shape[0]), traindata.label.values)
 trainidx,validx = list(folds)[conf.foldid]
trainpd = traindata.loc[trainidx,:].reset_index(drop=True)
 valpd = traindata.loc[validx,:].reset_index(drop=True)
 imgdir = datadir + '/train_images'
transform_train,transform_test = get_cassava_transform(conf)
 ds_train = ImageLoader(imgdir, train=True, transform=transform_train,pdata=trainpd)
 if conf.tta is None or conf.tta == 2:
ds_val = ImageLoader(imgdir, train=False, transform=transform_test,pdata=valpd,tta=conf.tta)
 ds_test = ImageLoader(testimgdir, train=False, transform=transform_test,pdata=testpd,tta=conf.tta)
 else:
ds_test = ImageLoader(testimgdir, train=False, transform=transform_train,pdata=testpd,tta=conf.tta)
 ds_val = ImageLoader(imgdir, train=False, transform=transform_train,pdata=valpd,tta=conf.tta)
 return ds_train,ds_val,ds_test,testpd
def get_params(model,conf=None):
if conf is not None and 'prams_group' in conf:
prams_group = conf.prams_group
lr_group = conf.lr_group
params = []
for pram,lr in zip(prams_group,lr_group):
params.append({'params':model.module.get_params(pram),'lr': lr})
 return params
return model.parameters() 
<set_options>
for index,row in df.iterrows() :
if "nan" != str(df.loc[index,"Cabin"]):
df.loc[index,"Cabin"] = df.loc[index,"Cabin"][0]
else:
df.loc[index,"Cabin"] = "X"
set_env(seed=0)
 foldid = 2
conf = edict({
'depth':50,
'pretrained':True,
'num_class':5,
'midlevel':False,
'datadir':'.. /input/cassava-leaf-disease-classification',
'dataset':'cassava',
'testing':False,
'tta': None,
'foldid':foldid,
'cropsize':448,
'netname':'resnet50',
'net_type':'resnet_ft',
'prams_group':['ftlayer','freshlayer'],
'lr_group':[0.001,0.01],
'lrstep':[20],
'lr': 0.001,
'epochs':25,
'lrgamma':0.1,
'criterion':'CrossEntropyLoss',
'reduction':'none',
'momentum':0.9,
'weight_decay':1e-4,
'pretrained':True,
'mixmethod': 'snapmix',
'prob': 1,
'beta': 5} 
)
 ds_train,ds_val,ds_test,testpd = get_dataset(conf)
 train_loader =data.DataLoader(ds_train, batch_size=16, shuffle= True, num_workers=8, pin_memory=True)
 val_loader =data.DataLoader(ds_val, batch_size=32, shuffle= False, num_workers=8, pin_memory=True)
 test_loader =data.DataLoader(ds_test, batch_size=32, shuffle= False, num_workers=16, pin_memory=True)
 model = eval(conf.net_type ).get_net(conf)
 model = nn.DataParallel(model ).cuda()
optimizer = torch.optim.SGD(get_params(model,conf),conf.lr,momentum=conf.momentum,weight_decay=conf.weight_decay,nesterov=True)
 criterion = nn.CrossEntropyLoss(reduction='none' ).cuda()
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,milestones=conf.lrstep, gamma=conf.lrgamma, last_epoch=-1)
 outfile = './'+conf.netname+'_f'+str(foldid)+'.pt'
if not os.path.isfile('.. /input/resfold2/'+outfile):
best_score = 0.
 for epoch in range(0, conf.epochs):
lr = optimizer.param_groups[0]['lr']
print("Epoch: [{} | {} LR: {}".format(epoch+1,conf.epochs,lr))
tmp_loss = train(train_loader, model, criterion, optimizer, conf)
 scheduler.step()
infostr = {'Epoch: {} train_loss: {}'.format(epoch+1,tmp_loss)}
print(infostr)
 with torch.no_grad() :
val_score,val_loss,mscore,ascore = validate(val_loader, model,criterion, conf)
 comscore = val_score
if conf.midlevel:
comscore = ascore
is_best = comscore > best_score
best_score = max(comscore,best_score)
 infostr = {'Epoch: {:.4f} loss: {:.4f},gs: {:.4f},bs:{:.4f},ms: {:.4f},as:{:.4f}'.format(epoch+1,val_loss,val_score,best_score,mscore,ascore)}
print(infostr)
 if is_best:
mdict = {'state_dict': model.module.state_dict() ,'epoch':epoch}
torch.save(mdict,outfile)
 else:
outfile = '.. /input/resfold2/'+outfile
load_checkpoint(model,outfile)
 with torch.no_grad() :
pres = predict(model,test_loader,conf.midlevel ).cpu().numpy()
testpd['label'] = pres
testpd.to_csv('submission.csv', index=False)
 print(testpd.head())
<install_modules>
df = pd.get_dummies(df, columns = ["Cabin"],prefix="Cabin" )
!pip install --quiet /kaggle/input/kerasapplications
!pip install --quiet /kaggle/input/efficientnet-git<set_options>
for index, row in df.iterrows() :
df.loc[index,'Title'] = row['Name'].split(".")[0].split(", ")[1]
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 0
seed_everything(seed)
 warnings.filterwarnings('ignore' )<define_variables>
frequent_titles = ['Mr','Mrs','Miss','Master']
for index, row in df.iterrows() :
if df.loc[index,'Title'] not in frequent_titles:
df.loc[index,'Title'] = "Rare"
BATCH_SIZE = 4 * REPLICAS
HEIGHT = 512
WIDTH = 512
CHANNELS = 3
N_CLASSES = 5
TTA_STEPS = 3<normalization>
df.drop(labels = ["Name"], axis = 1, inplace = True )
def get_name(file_path):
parts = tf.strings.split(file_path, os.path.sep)
 name = parts[-1]
return name
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
return image
def center_crop(image):
image = tf.reshape(image, [600, 800, CHANNELS])
 h, w = image.shape[0], image.shape[1]
if h > w:
image = tf.image.crop_to_bounding_box(image,(h - w)// 2, 0, w, w)
 else:
image = tf.image.crop_to_bounding_box(image, 0,(w - h)// 2, h, h)
 image = tf.image.resize(image, [HEIGHT, WIDTH])
 return image
def resize_image(image, label):
image = tf.image.resize(image, [HEIGHT, WIDTH])
 image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])
 return image, label
def process_path(file_path):
name = get_name(file_path)
 img = tf.io.read_file(file_path)
 img = decode_image(img)
 return img, name
def get_dataset(files_path, shuffled=False, tta=False, extension='jpg'):
dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)
 dataset = dataset.map(process_path, num_parallel_calls=AUTO)
 if tta:
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.map(resize_image, num_parallel_calls=AUTO)
 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 )<train_model>
for index,row in df.iterrows() :
if df.loc[index,"Ticket"].isdigit() :
df.loc[index,"Ticket"] = "X"
else:
df.loc[index,"Ticket"] = df.loc[index,"Ticket"].replace(".","" ).replace("/","" ).strip().split(' ')[0]
df = pd.get_dummies(df, columns = ["Ticket"], prefix="T" )
model_path_list = glob.glob('/kaggle/input/cassava-leaf-disease-tpu-tensorflow-training/*.h5')
 model_path_list.sort()
print('Models to predict:')
 print(*model_path_list, sep='
' )<train_model>
df = pd.get_dummies(df, columns = ["Pclass"],prefix="Pc")
 df = pd.get_dummies(df, columns = ["Embarked"],prefix="Embarked")
 df = pd.get_dummies(df, columns = ["Title"],prefix="Title")
 df.drop(labels = ["PassengerId"], axis = 1, inplace = True )
model_path_list_2 = glob.glob('/kaggle/input/cassava-leaf-disease-training-with-tpu-v2-pods/*.h5')
 model_path_list_2.sort()
print('Models to predict:')
 print(*model_path_list_2, sep='
' )<choose_model_class>
train = df[:train.shape[0]]
test = df[train.shape[0]:]
test.drop(labels=["Survived"],axis = 1,inplace=True)
 Y_train = train["Survived"]
X_train = train.drop(labels = ["Survived"],axis = 1 )
def model_fn(input_shape, N_CLASSES):
inputs = L.Input(shape=input_shape, name='inputs')
 base_model = efn.EfficientNetB3(input_tensor=inputs,
include_top=False,
weights=None,
pooling='avg')
 model = tf.keras.Sequential([
base_model,
L.Dropout (.25),
L.Dense(N_CLASSES, activation='softmax', name='output')
 ])
 return model
def model_fn_2(input_shape, N_CLASSES):
inputs = L.Input(shape=input_shape, name='inputs')
 base_model = efn.EfficientNetB4(input_tensor=inputs,
include_top=False,
weights=None,
pooling='avg')
 x = L.Dropout (.5 )(base_model.output)
 output = L.Dense(N_CLASSES, activation='softmax', name='output' )(x)
 model = Model(inputs=inputs, outputs=output)
 return model
with strategy.scope() :
model = model_fn(( None, None, CHANNELS), N_CLASSES)
 model_2 = model_fn_2(( None, None, CHANNELS), N_CLASSES)
 model.summary() 
<predict_on_test>
random_state = 2
clf = ['SVC','RandomForest','GradientBoosting','KNeighbors','LogisticRegression']
classifiers = []
classifiers.append(SVC(random_state=random_state))
classifiers.append(RandomForestClassifier(random_state=random_state))
classifiers.append(GradientBoostingClassifier(random_state=random_state))
classifiers.append(KNeighborsClassifier())
 classifiers.append(LogisticRegression(random_state = random_state))
files_path = f'{database_base_path}test_images/'
test_preds = np.zeros(( len(os.listdir(files_path)) , N_CLASSES))
print('First model')
 for model_path in model_path_list:
print(model_path)
 K.clear_session()
model.load_weights(model_path)
 if TTA_STEPS > 0:
test_ds = get_dataset(files_path, tta=True)
 for step in range(TTA_STEPS):
print(f'TTA step {step+1}/{TTA_STEPS}')
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model.predict(x_test)/(TTA_STEPS * len(model_path_list))
else:
test_ds = get_dataset(files_path, tta=False)
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model.predict(x_test)/ len(model_path_list)
 print('
Second model')
 for model_path in model_path_list_2:
print(model_path)
 K.clear_session()
model_2.load_weights(model_path)
 if TTA_STEPS > 0:
test_ds = get_dataset(files_path, tta=True)
 for step in range(TTA_STEPS):
print(f'TTA step {step+1}/{TTA_STEPS}')
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model_2.predict(x_test)/(TTA_STEPS * len(model_path_list))
else:
test_ds = get_dataset(files_path, tta=False)
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model_2.predict(x_test)/ len(model_path_list)
 test_preds = np.argmax(test_preds, axis=-1)
 image_names = [img_name.numpy().decode('utf-8')for img, img_name in iter(test_ds.unbatch())]<save_to_csv>
clf_scores = []
for i in range(len(classifiers)) :
scores = cross_val_score(classifiers[i], X_train, y = Y_train, scoring = "accuracy", n_jobs=-1)
 clf_scores.append([clf[i],scores.mean() ,scores.std() ] )
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False)
 display(submission.head() )<install_modules>
df_scores = pd.DataFrame(clf_scores)
 df_scores.columns = ['Model','Acc_Mean','Acc_Std']
df_scores
!pip install.. /input/timm031/timm-0.3.1-py3-none-any.whl
sub = 0
if sub==0:
!pip install --upgrade pip adabound<import_modules>
SVMC = SVC(probability=True)
 svc_param_grid = {'kernel': ['rbf'],
'gamma': [ 0.001, 0.01, 0.1, 1],
'C': [1, 10, 50, 100,200,300, 1000]}
gsSVMC = GridSearchCV(SVMC,param_grid = svc_param_grid,scoring="accuracy", n_jobs= -1, verbose = 1)
 gsSVMC.fit(X_train,Y_train)
 SVMC_best = gsSVMC.best_estimator_
gsSVMC.best_score_
import numpy as np
import pandas as pd
import timm
import os
import matplotlib.pyplot as plt
import cv2
import sys
from sklearn import model_selection, metrics
import torch
from PIL import Image
from tensorflow.keras import models, layers
from PIL import ImageEnhance, ImageOps
import pdb
import torchvision.transforms as transforms
import adabound
import random 
<define_variables>
GBC = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [100,200,300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, scoring="accuracy", n_jobs= -1, verbose = 1)
 gsGBC.fit(X_train,Y_train)
 GBC_best = gsGBC.best_estimator_
print("Best Score: ",gsGBC.best_score_ )
BASE_DIR = '.. /input/cassava-leaf-disease-classification'
TRAIN_PATH = BASE_DIR+"/train_images/"
TEST_PATH = BASE_DIR+"/test_images/"<train_model>
RFC = RandomForestClassifier()
rf_param_grid = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,300],
"criterion": ["gini"]}
gsRFC = GridSearchCV(RFC,param_grid = rf_param_grid,scoring="accuracy", n_jobs= -1, verbose = 1)
 gsRFC.fit(X_train,Y_train)
 RFC_best = gsRFC.best_estimator_
print("Best Score: ",gsRFC.best_score_ )
def read_image(path,label):
image_data = cv2.imread(path)
 plt.title('label:{}'.format(label))
plt.imshow(image_data)
 return image_data
class CassavaDataset(torch.utils.data.Dataset):

 def __init__(self, df, data_path, mode="train", transforms=None):
super().__init__()
self.df_data = df.values
self.data_path = data_path
self.transforms = transforms
self.mode = mode
self.data_dir = "train_images" if mode == "train" else "test_images"
def __len__(self):
return len(self.df_data)
 def __getitem__(self, index):
img_name, label = self.df_data[index]
img = Image.open(img_name ).convert("RGB")
 if self.transforms is not None:
img = self.transforms(img)
 return img, label
class CassvaImgClassifier(torch.nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = torch.nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x
class CassavaNet(torch.nn.Module):
def __init__(self,model_name):
super().__init__()
backbone = timm.create_model(model_name, pretrained=True)
 n_features = backbone.fc.in_features
self.backbone = torch.nn.Sequential(*backbone.children())[:-2]
self.classifier = torch.nn.Linear(n_features, 5)
 self.pool = torch.nn.AdaptiveAvgPool2d(( 1, 1))
def forward_features(self, x):
x = self.backbone(x)
 return x
def forward(self, x):
feats = self.forward_features(x)
 x = self.pool(feats ).view(x.size(0), -1)
 x = self.classifier(x)
 return x, feats<train_model>
votingC = VotingClassifier(estimators=[('RandomForest', RFC_best),('SVC', SVMC_best),('GradientBoosting',GBC_best)], voting='soft', n_jobs=-1)
 votingC = votingC.fit(X_train, Y_train )
def train(net, epoch, trainLoader, optimizer, criterion):
sum_loss = 0
total = 0
correct = 0
net.train()
for i, data in enumerate(trainLoader, 0):
length = len(data)
 input, target = data
input, target = input.to(device), target.to(device)
 optimizer.zero_grad()
output= net(input)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
sum_loss += loss.item()
_, predicted = torch.max(output.data, 1)
 total += target.size(0)
 correct += predicted.eq(target.data ).cpu().sum()
if i % 49 == 0 and i != 0:
log_string('[epoch:%d, batch:%d] Loss: %.03f | Acc: %.3f%% ' %(
 epoch + 1, i + 1, sum_loss / total, 100.* float(correct)/ float(total)))
 log_string('[epoch:%d] Loss: %.03f | Acc: %.3f%% ' %(
 epoch + 1, sum_loss / total, 100.* float(correct)/ float(total)))
 return float(correct)/ float(total)
 def valid(net, EPOCH, validLoader):
total = 0
correct = 0
net.eval()
with torch.no_grad() :
correct = 0
total = 0
for data in validLoader:
net.eval()
images, labels = data
images, labels = images.to(device), labels.to(device)
 outputs = net(images)
 _, predicted = torch.max(outputs.data, 1)
 total += labels.size(0)
 correct +=(predicted == labels ).cpu().sum()
log_string('测试分类准确率为：%.3f%%' %(100.* float(correct)/ float(total)))
 return float(correct)/ float(total)
 def log_string(out_str):
LOG_FOUT.write(out_str + '
')
 LOG_FOUT.flush()
print(out_str )<load_from_csv>
predictions = pd.Series(votingC.predict(test),name="Survived")
 results = pd.concat([test_ids,predictions],axis=1)
 results = results.astype(int )
lr = 1e-4
BATCH_SIZE=8
IMG_SIZE=512
EPOCH=10
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 df = pd.read_csv(os.path.join(BASE_DIR,'train.csv'))
df.image_id = df.image_id.apply(lambda x : TRAIN_PATH+x)
 LOG_FOUT = open(os.path.join('./', 'log_train.txt'), 'w')
 if os.path.exists('./models')==0:
os.mkdir('./models' )<create_dataframe>
results.to_csv("submission.csv",index=False )