cs231_features

features 作业

features原理

  1. 改变只将原图的像素作为特征表示,将原图的hog特征,作为该图的特征表示
  2. 改变只将原图的像素作为特征表示,将原图的颜色直方图作为该图的特征表示

features.ipynb

  1. 训练SVM on features

    # Use the validation set to tune the learning rate and regularization strength
    
    from cs231n.classifiers.linear_classifier import LinearSVM
    
    learning_rates = [1e-9, 1e-8, 1e-7]
    regularization_strengths = [5e4, 5e5, 5e6]
    
    results = {}
    best_val = -1
    best_svm = None
    
    pass
    ################################################################################
    # TODO:                                                                        #
    # Use the validation set to set the learning rate and regularization strength. #
    # This should be identical to the validation that you did for the SVM; save    #
    # the best trained classifer in best_svm. You might also want to play          #
    # with different numbers of bins in the color histogram. If you are careful    #
    # you should be able to get accuracy of near 0.44 on the validation set.       #
    ################################################################################
    for cur_lr in learning_rates:
        for cur_reg in regularization_strengths:
            svm = LinearSVM()
            svm.train(X_train_feats,y_train, learning_rate=cur_lr, reg=cur_reg,num_iters=1500,verbose=False)
            
            y_train_pred = svm.predict(X_train_feats)
            train_acc = np.mean(y_train==y_train_pred)
            
            y_val_pred = svm.predict(X_val_feats)
            val_acc = np.mean(y_val==y_val_pred)
            
            results[(cur_lr,cur_reg)] = (train_acc, val_acc)
            
            if val_acc > best_val:
                best_val = val_acc
                best_svm = svm
        
    ################################################################################
    #                              END OF YOUR CODE                                #
    ################################################################################
    
    # Print out results.
    for lr, reg in sorted(results):
        train_accuracy, val_accuracy = results[(lr, reg)]
        print('lr %e reg %e train accuracy: %f val accuracy: %f' % (
                    lr, reg, train_accuracy, val_accuracy))
        
    print('best validation accuracy achieved during cross-validation: %f' % best_val)
    

    最后结果为:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-98skmPKz-1618559810747)(C:\Users\vpmas\AppData\Roaming\Typora\typora-user-images\image-20210307202623873.png)]

    最终在test上的结果为

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-StzLK0Kt-1618559810749)(C:\Users\vpmas\AppData\Roaming\Typora\typora-user-images\image-20210307202656367.png)]

    1. 利用特征的神经网络

      from cs231n.classifiers.neural_net import TwoLayerNet
      
      input_dim = X_train_feats.shape[1]
      hidden_dim = 200
      num_classes = 10
      
      
      best_net = None
      
      ################################################################################
      # TODO: Train a two-layer neural network on image features. You may want to    #
      # cross-validate various parameters as in previous sections. Store your best   #
      # model in the best_net variable.                                              #
      ################################################################################
      bestacc = 0
      for hidden_size in [hidden_dim]:
          for learning_rate in [1e-1,5e-1]:
              for learning_rate_decay in [0.999]:
                  for reg in [3e-4,1e-3,3e-3]:
                      net = TwoLayerNet(input_dim, hidden_dim, num_classes)
                      # Train the network
                      stats = net.train(X_train_feats, y_train, X_val_feats, y_val,
                                  num_iters=3000, batch_size=500,
                                  learning_rate=learning_rate, learning_rate_decay=learning_rate_decay,
                                  reg=reg, verbose=False)
      
                      # Predict on the validation set
                      val_acc = (net.predict(X_val_feats) == y_val).mean()
                      print('hidden_size = %d,learning_rate = %f,learning_rate_decay = %f,reg = %f,Validation accuracy =%f '%(hidden_size,learning_rate,learning_rate_decay,reg,val_acc))
                      if bestacc<val_acc:
                          bestacc = val_acc
                          best_net = net
      ################################################################################
      #                              END OF YOUR CODE                                #
      ################################################################################
      

      最终结果为:

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6lOGk5Nt-1618559810749)(C:\Users\vpmas\AppData\Roaming\Typora\typora-user-images\image-20210307202815449.png)]

questions

Inline question 1:

Describe the misclassification results that you see. Do they make sense?
错误的分类结果大部分与此类的可能出现的背景相似,因为这里是通过hog特征进行分类判断,即对应位置梯度接近的图像将被分为一类,因此具有相同hog特征的类将被分为一类,所以出现这类的情况比较合理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值