Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Building a Deep Learning Model to Perform Image Classification with the American Sign Language Dataset

Now, it's your turn. You'll need to use what we have learned before to build a model to perform image classification with American Sign Language dataset.

Requirement:

  • The data preparation and the training functions are given.
  • You'll need to use a model of your choice and to perform the classification, aka predict the correct categories. Modify the model function to specify your model.
  • Then use the training functions to train your model with epoch=10.
  • This is an individual project. You can only ask questions to the instructor and only in the project QA session.



You can modify the cell below to include additional libraries you want to use.

In [1]:
importtorch.nnasnnimportpandasaspdimporttorchfromtorch.optimimportAdamfromtorch.utils.dataimportDataset,DataLoaderdevice=torch.device("cuda"iftorch.cuda.is_available()else"cpu")torch.cuda.is_available()
Out[1]:
True

1. Data Preparation

Do not change anything in this part.

In [ ]:
fromgoogle.colabimportdrivedrive.mount('/content/drive')# mount your google drive
Mounted at /content/drive
In [ ]:
train_df=pd.read_csv("/content/drive/MyDrive/STA138_WI26/sign_mnist_train.csv")valid_df=pd.read_csv("/content/drive/MyDrive/STA138_WI26/sign_mnist_valid.csv")

If you have erros in running the above cell, drag the folder STA138_WI26 to MyDrive.

The following plots give you an idea of the feature images and the labels.

In [ ]:
y_train=train_df.pop('label')y_valid=valid_df.pop('label')x_train=train_df.valuesx_valid=valid_df.values
In [ ]:
importmatplotlib.pyplotaspltplt.figure(figsize=(40,40))num_images=20foriinrange(num_images):row=x_train[i]label=y_train[i]image=row.reshape(28,28)plt.subplot(1,num_images,i+1)plt.title(label,fontdict={'fontsize':30})plt.axis('off')plt.imshow(image,cmap='gray')

In [ ]:
IMG_HEIGHT=28IMG_WIDTH=28IMG_CHS=1classMyDataset(Dataset):def__init__(self,base_df):x_df=base_df.copy()# Some operations below are in-placey_df=x_df.pop('label')x_df=x_df.values/255# Normalize values from 0 to 1x_df=x_df.reshape(-1,IMG_CHS,IMG_WIDTH,IMG_HEIGHT)self.xs=torch.tensor(x_df).float().to(device)self.ys=torch.tensor(y_df).to(device)def__getitem__(self,idx):x=self.xs[idx]y=self.ys[idx]returnx,ydef__len__(self):returnlen(self.xs)
In [ ]:
BATCH_SIZE=32train_df=pd.read_csv("/content/drive/MyDrive/STA138_WI26/sign_mnist_train.csv")valid_df=pd.read_csv("/content/drive/MyDrive/STA138_WI26/sign_mnist_valid.csv")train_data=MyDataset(train_df)train_loader=DataLoader(train_data,batch_size=BATCH_SIZE,shuffle=True)train_N=len(train_loader.dataset)valid_data=MyDataset(valid_df)valid_loader=DataLoader(valid_data,batch_size=BATCH_SIZE)valid_N=len(valid_loader.dataset)
In [ ]:
batch=next(iter(train_loader))batchbatch[0].shape
Out[8]:
torch.Size([32, 1, 28, 28])

2. Creating the Model

SPECIFY your model below.

  • A basic convolutionary neural network model is provided.
  • You should change the model architecture (you can change the structure of the convolutionary neural network or use a feedforward neural network) or change the model parameters to get a better performance.
In [ ]:
IMG_CHS=1# this is the number of channels for the imagen_classes=24kernel_size=3flattened_img_size=10*14*14## Specify your own model by changing the variable modelmodel=nn.Sequential(# convolutionnn.Conv2d(IMG_CHS,10,kernel_size,stride=1,padding=1),nn.BatchNorm2d(10),nn.ReLU(),nn.MaxPool2d(2,stride=2),# 10 x 14 x 14# dense layernn.Flatten(),nn.Linear(flattened_img_size,64),nn.Dropout(.3),nn.ReLU(),nn.Linear(64,n_classes))model.to(device)
Out[9]:
Sequential( (0): Conv2d(1, 10, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (1): BatchNorm2d(10, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (2): ReLU() (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (4): Flatten(start_dim=1, end_dim=-1) (5): Linear(in_features=1960, out_features=64, bias=True) (6): Dropout(p=0.3, inplace=False) (7): ReLU() (8): Linear(in_features=64, out_features=24, bias=True) )

3. Model Training

Do not change anything below.

In [ ]:
loss_function=nn.CrossEntropyLoss()optimizer=Adam(model.parameters())train_N=len(train_loader.dataset)valid_N=len(valid_loader.dataset)defget_batch_accuracy(output,y,N):pred=output.argmax(dim=1,keepdim=True)correct=pred.eq(y.view_as(pred)).sum().item()returncorrect/Ndeftrain():loss=0accuracy=0model.train()forx,yintrain_loader:x,y=x.to(device),y.to(device)output=model(x)optimizer.zero_grad()batch_loss=loss_function(output,y)batch_loss.backward()optimizer.step()loss+=batch_loss.item()accuracy+=get_batch_accuracy(output,y,train_N)print('Train - Loss: {:.4f} Accuracy: {:.4f}'.format(loss,accuracy))defvalidate():loss=0accuracy=0model.eval()withtorch.no_grad():forx,yinvalid_loader:x,y=x.to(device),y.to(device)output=model(x)loss+=loss_function(output,y).item()accuracy+=get_batch_accuracy(output,y,valid_N)print('Valid - Loss: {:.4f} Accuracy: {:.4f}'.format(loss,accuracy))

Run the following cell the check the performance of your model.

In [ ]:
epochs=10forepochinrange(epochs):print('Epoch: {}'.format(epoch))train()validate()
Epoch: 0 Train - Loss: 850.7248 Accuracy: 0.6923 Valid - Loss: 115.6169 Accuracy: 0.8119 Epoch: 1 Train - Loss: 210.5101 Accuracy: 0.9210 Valid - Loss: 130.1884 Accuracy: 0.8165 Epoch: 2 Train - Loss: 126.5620 Accuracy: 0.9500 Valid - Loss: 146.7893 Accuracy: 0.8296 Epoch: 3 Train - Loss: 96.6071 Accuracy: 0.9610 Valid - Loss: 136.8287 Accuracy: 0.8475 Epoch: 4 Train - Loss: 79.0805 Accuracy: 0.9675 Valid - Loss: 155.2563 Accuracy: 0.8392 Epoch: 5 Train - Loss: 74.6064 Accuracy: 0.9699 Valid - Loss: 172.1851 Accuracy: 0.8344 Epoch: 6 Train - Loss: 68.6347 Accuracy: 0.9712 Valid - Loss: 212.9262 Accuracy: 0.8305 Epoch: 7 Train - Loss: 61.2475 Accuracy: 0.9744 Valid - Loss: 199.9772 Accuracy: 0.8298 Epoch: 8 Train - Loss: 60.1666 Accuracy: 0.9746 Valid - Loss: 229.3628 Accuracy: 0.8182 Epoch: 9 Train - Loss: 57.8113 Accuracy: 0.9751 Valid - Loss: 203.6735 Accuracy: 0.8437

Submission Instructions

After you have chosen your final model.

  • Please click Runtime->Restart session and run all.
  • Click File -> Print -> Save as PDF.
  • Change the PDF file name to your Firstname_LastName.pdf (say Justin_Fields.pdf) and upload it to the box.ucdavis.edu submission folder.
  • Copy the output from the previous cell, for example,Epoch: 0 Train - Loss: 1053.0395 Accuracy: 0.6117 Valid - Loss: 165.4801 Accuracy: 0.7539 Epoch: 1 Train - Loss: 402.2355 Accuracy: 0.8408 Valid - Loss: 159.5908 Accuracy: 0.7818 Epoch: 2 Train - Loss: 271.9850 Accuracy: 0.8895 Valid - Loss: 160.4108 Accuracy: 0.8069 Epoch: 3 Train - Loss: 225.0550 Accuracy: 0.9048 Valid - Loss: 195.5503 Accuracy: 0.7927 Epoch: 4 Train - Loss: 185.9369 Accuracy: 0.9219 Valid - Loss: 238.5131 Accuracy: 0.7789 Epoch: 5 Train - Loss: 161.1346 Accuracy: 0.9307 Valid - Loss: 189.5530 Accuracy: 0.8211 Epoch: 6 Train - Loss: 150.2623 Accuracy: 0.9355 Valid - Loss: 232.6281 Accuracy: 0.8020 Epoch: 7 Train - Loss: 135.8071 Accuracy: 0.9416 Valid - Loss: 252.1603 Accuracy: 0.8246 Epoch: 8 Train - Loss: 124.8701 Accuracy: 0.9459 Valid - Loss: 240.1892 Accuracy: 0.8194 Epoch: 9 Train - Loss: 115.9816 Accuracy: 0.9500 Valid - Loss: 264.5139 Accuracy: 0.8105And put it in a txt file named submission.txt.
  • Submit the submission.txt file to gradescope. Your grade will show up within one minute after you make the submission on gradescope. If not, please check your file and make sure the format is correct (no additional lines or spaces).

The grading will be based on the performance of your final model. The key metric is min(Train Accuracy, Valid Accuracy) - abs(Train Accuracy - Valid Accuracy).

Core Courses (36 units)
STA 135 Multivariate Data Analysis (4 units)
STA 141A Fundamentals of Statistical Data Science (4 units)
STA 200A Introduction to Probability Theory
STA 200B Introduction to Mathematical Statistics (4 units each)
STA 206, 207 Statistical Methods and Research (4 units each)
STA 208 Statistical Methods in Machine Learning (4 units)
STA 209 Optimization for Big Data Analytics (4 units)
Plus one of the following two courses:
STA 242 Introduction to Statistical Programming (4 units), or
STA 243 Computational Statistics (4 units)

Elective Courses (total 12 units):
At least one course selected from the following:
STA 137 Applied Time Series Analysis (4 units)
STA 138 Analysis of Categorical Data (4 units)
STA 141B Data & Web Technologies for Data Anlysis (4 units) (MS students strongly encouraged to take STA 220 instead)
STA 141C Big Data & High Performance Statistical Computing (4 units) (MS students strongly encouraged to take STA 221 instead)
STA 144 Sampling Theory of Surveys (4 units)
STA 145 Bayesian Statistical Inference (4 units)
STA 260 Statistical Practice and Data Analysis (3 units)
Or any graduate level statistics course (4 units)

At least one course selected from the following:
ECS 122A, B Algorithm Design and Analysis (4 units each)
ECS 165A, B Database Systems (4 units each)
ECS 170 Artificial Intelligence (4 units)
ECS 171 Machine Learning (4 units)
ECS 289G Special Topics in Computer Science (4 units)