基础_模型迁移_CBIR_augmentation

简介: 在之前我们做过这样的研究:5图分类CBIR问题各不相同的 5类的图形,每类100张import numpy as npfrom keras.datasets import mnistimport gcfrom keras.
在之前我们做过这样的研究: 5图分类 CBIR问题
各不相同的 5类的图形,每类100张
import numpy as np
from keras.datasets  import mnist
import gc

from keras.models  import Sequential, Model
from keras.layers  import Input, Dense, Dropout, Flatten
from keras.layers.convolutional  import Conv2D, MaxPooling2D
from keras.applications.vgg16  import VGG16
from keras.optimizers  import SGD
from keras.utils.data_utils  import get_file
import cv2
import h5py as h5py 
import numpy as np

import  os
import  math
from matplotlib  import pyplot as plt

#全局变量
RATIO  =  0. 2
train_dir  =  'D:/dl4cv/datesets/littleCBIR/'

#根据分类总数确定one-hot总类
NUM_DENSE  =  5
#训练总数
epochs  =  10

def tran_y(y) : 
    y_ohe  = np.zeros(NUM_DENSE) 
    y_ohe[y]  =  1 
     return y_ohe

#根据Ratio获得训练和测试数据集的图片地址和标签
##生成数据集,本例先验3**汽车、4**恐龙、5**大象、6**花、7**马
def get_files(file_dir, ratio) :
     '' '
    Args:
        file_dir: file directory
    Returns:
        list of images and labels
    '
''
    image_list  = []
    label_list  = []
     for file  in  os.listdir(file_dir) :
         if file[ 0 : 1] == '3' :
            image_list.append(file_dir  + file)
            label_list.append( 0)
         elif file[ 0 : 1] == '4' :
            image_list.append(file_dir  + file)
            label_list.append( 1)
         elif file[ 0 : 1] == '5' :
            image_list.append(file_dir  + file)
            label_list.append( 2)
         elif file[ 0 : 1] == '6' :
            image_list.append(file_dir  + file)
            label_list.append( 3)
         else :
            image_list.append(file_dir  + file)
            label_list.append( 4)
     print( '数据集导入完毕')
     #图片list和标签list
     #hstack 水平(按列顺序)把数组给堆叠起来
    image_list  = np.hstack(image_list)
    label_list  = np.hstack(label_list)
    
    temp  = np. array([image_list, label_list])
    temp  = temp.transpose()
    np. random.shuffle(temp)   
    
    all_image_list  = temp[ :0]
    all_label_list  = temp[ :1]
    
    n_sample  =  len(all_label_list)
     #根据比率,确定训练和测试数量
    n_val  =  math.ceil(n_sample *ratio)  # number of validation samples
    n_train  = n_sample  - n_val  # number of trainning samples
    tra_images  = []
    val_images  = []
     #按照0-n_train为tra_images,后面位val_images的方式来排序
    
     for index  in  range(n_train) :
        image  = cv2.imread(all_image_list[index])
         #灰度,然后缩放
        image  = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
        image  = cv2.resize(image,( 48, 48)) #到底在这个地方修改,还是在后面修改,需要做具体实验
        tra_images.append(image)

    tra_labels  = all_label_list[ :n_train]
    tra_labels  = [ int( float(i))  for i  in tra_labels]

     for index  in  range(n_val) :
        image  = cv2.imread(all_image_list[n_train +index])
         #灰度,然后缩放
        image  = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
        image  = cv2.resize(image,( 32, 32))
        val_images.append(image)

    val_labels  = all_label_list[n_train :]
    val_labels  = [ int( float(i))  for i  in val_labels]
     return np. array(tra_images),np. array(tra_labels),np. array(val_images),np. array(val_labels)

# colab+VGG要求至少48像素在现有数据集上,已经能够完成不错情况
ishape = 48
#(X_train, y_train), (X_test, y_test) = mnist.load_data() 
#获得数据集
#X_train, y_train, X_test, y_test = get_files(train_dir, RATIO)
#保持数据
##np.savez("D:\\dl4cv\\datesets\\littleCBIR.npz",X_train=X_train,y_train=y_train,X_test=X_test,y_test=y_test)
#读取数据
 
path = 'littleCBIR.npz'
#https://github.com/jsxyhelu/GOCW/raw/master/littleCBIR.npz
path  = get_file(path,origin = 'https://github.com/jsxyhelu/GOCW/raw/master/littleCBIR.npz')
= np.load(path)
X_train, y_train  = f[ 'X_train'], f[ 'y_train']
X_test, y_test  = f[ 'X_test'], f[ 'y_test']


X_train  = [cv2.cvtColor(cv2.resize(i, (ishape, ishape)), cv2.COLOR_GRAY2BGR)  for i  in X_train] 
X_train  = np.concatenate([arr[np.newaxis]  for arr  in X_train]).astype( 'float32'
X_train  /=  255. 0

X_test  = [cv2.cvtColor(cv2.resize(i, (ishape, ishape)), cv2.COLOR_GRAY2BGR)  for i  in X_test] 
X_test  = np.concatenate([arr[np.newaxis]  for arr  in X_test]).astype( 'float32')
X_test  /=  255. 0

y_train_ohe  = np. array([tran_y(y_train[i])  for i  in  range( len(y_train))]) 
y_test_ohe  = np. array([tran_y(y_test[i])  for i  in  range( len(y_test))])
y_train_ohe  = y_train_ohe.astype( 'float32')
y_test_ohe  = y_test_ohe.astype( 'float32')


model_vgg  = VGG16(include_top  =  False, weights  =  'imagenet', input_shape  = (ishape, ishape,  3)) 
#for i, layer in enumerate(model_vgg.layers): 
#    if i<20:
for layer  in model_vgg.layers :
        layer.trainable  =  False
model  = Flatten()(model_vgg.output) 
model  = Dense( 4096, activation = 'relu', name = 'fc1')(model)
model  = Dense( 4096, activation = 'relu', name = 'fc2')(model)
model  = Dropout( 0. 5)(model)
model  = Dense(NUM_DENSE, activation  =  'softmax', name = 'prediction')(model) 
model_vgg_pretrain  = Model(model_vgg. input, model, name  =  'vgg16_pretrain')
#model_vgg_pretrain.summary()
print( "vgg准备完毕\n")
sgd  = SGD(lr  =  0. 05, decay  =  1e - 5
model_vgg_pretrain. compile(loss  =  'categorical_crossentropy', optimizer  = sgd, metrics  = [ 'accuracy'])
print( "vgg开始训练\n")
log  = model_vgg_pretrain.fit(X_train, y_train_ohe, validation_data  = (X_test, y_test_ohe), epochs  = epochs, batch_size  =  64)

score  = model_vgg_pretrain.evaluate(X_test, y_test_ohe, verbose = 0)
print( 'Test loss:', score[ 0])
print( 'Test accuracy:', score[ 1])

plt.figure( 'acc')  
plt.subplot( 211)  
plt.plot(log.history[ 'acc'], 'r--',label = 'Training Accuracy')  
plt.plot(log.history[ 'val_acc'], 'r-',label = 'Validation Accuracy')  
plt.legend(loc = 'best')  
plt.xlabel( 'Epochs')  
plt.axis([ 0, epochs,  0. 51])  
plt.figure( 'loss')  
plt.subplot( 212)  
plt.plot(log.history[ 'loss'], 'b--',label = 'Training Loss')  
plt.plot(log.history[ 'val_loss'], 'b-',label = 'Validation Loss')  
plt.legend(loc = 'best')  
plt.xlabel( 'Epochs')  
plt.axis([ 0, epochs,  01])  
  
plt.show() 
os.system( "pause")

epoch = 10

Test loss: 0.5930496269464492
Test accuracy: 0.83

epoch = 30

Test loss: 1.254318968951702
Test accuracy: 0.68



原图修正
Test loss: 0.2156761786714196
Test accuracy: 0.93
 

应该说,主要代码问题不大,也摸索出来了系列方法,但是需要进一步丰富。今天,我在之前的基础上继续研究,解决以下问题:
1、在 5Type4CBIR( https://www.kaggle.com/jsxyhelu/5type4cbir) 做训练;
2、使用aumentation方法,提高前期效果;
3、 不仅仅是曲线, 结果要以可视化方法绘制出来;
4、最终的结果要能够下载下来;

1、在 5Type4CBIR( https://www.kaggle.com/jsxyhelu/5type4cbir) 做训练
基于之前的积累,我已经将数据集变成了npz格式,可以直接使用keras进行下载。为此,我专(peng)门(qiao)在kaggle上创建了这个数据集,立刻感到B格高涨。
kaggle上传太慢,npz的数据仍然从github中下载;

2、使用aumentation方法,提高前期效果;
如果从一般意义上来说,aumentation方法的引入肯定能够提高处理效果的,特别对于这样一个数据集比较小的问题来说尤其是这样。从目前这个意义上来看:
img_generator.fit(X_train)
# fits the model_2 on batches with real-time data augmentation:
log = model_vgg_pretrain.fit_generator(img_generator.flow(X_train,y_train_ohe, batch_size= 64),
steps_per_epoch= len(X_train), epochs=epochs)
score = model_vgg_pretrain.evaluate(X_test, y_test_ohe, verbose= 0)
简单几行代码,就可以解决这里的问题。如果确实有效,那么将被复用;
在不generate下,达到95
Epoch 30/30
400/400 [==============================] - 15s 36ms/step - loss: 0.1731 - acc: 0.9375 - val_loss: 0.1910 - val_acc: 0.9500
Test loss: 0.19096931144595147
Test accuracy: 0.95
x

​在对generate进一步理解后,我认为现在的结果应该能够更高,经过夜间训练,得到这样结果
 
Epoch 10/10
18/400 [>.............................] - ETA: 20:44 - loss: 0.0221 - acc: 0.9935
400/400 [==============================] - 1275s 3s/step - loss: 0.0449 - acc: 0.9875 - val_loss: 0.0667 - val_acc: 0.9700
Test loss: 0.06673358775209635
Test accuracy: 0.97


其中,核心代码及参数为:
log = model_vgg_pretrain.fit_generator(img_generator.flow(X_train,y_train_ohe, batch_size = 128), steps_per_epoch = 400, epochs =10,validation_data =(X_test, y_test_ohe),workers = 4)
反观数据,可以发现在epoch = 7 的时候,就已经达到最好;这个曲线较前面的训练更为平滑,总体质量也更高。98的准确率对于消费级的应用是一个很好的结果。
augumentation的方法将会被经常复用。
3、不仅仅是曲线,结果要以可视化方法绘制出来;
里的意思也很直接,就是我要真实地去验证数据,要把模型结果显示出来,而不仅仅是在模型里面fit。这个显示首先是在juypter里面直观的观看。这个是给我来看的。一个是要在后面的结果应用中能够下载,并且被我现在的软件来使用。
但是目前这一步无法完成,因为前期我使用OpenCV读入图像、预处理、压制成npz格式的,目前我认为这个效果很好。OpenCV的 Mat格式目前无法在Juypter中进行显示。
4、最终的结果要能够下载下来;
colab中有一段可以下载的代码的,翻阅了一些资料,解决了这个问题:

# Install the PyDrive wrapper & import libraries.
# This only needs to be done once in a notebook.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Create & upload a text file.
uploaded = drive.CreateFile()
uploaded.SetContentFile( '5type4cbirMODEL.h5')
uploaded.Upload()
print( 'Uploaded file with ID {}'.format(uploaded.get( 'id')))


但是这样一个200m的东西下载下来也比较费时间,如果能够结合google driver直接使用是最好的。google driver就相当于colab的外存储。调用的代码应该类似这样:

# Install the PyDrive wrapper & import libraries.
# This only needs to be done once per notebook.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
#根据文件名进行下载
file_id = '1qjxAm_QiXdSqBmyIoPl3bfnyLNJxwKo9'
downloaded = drive.CreateFile({ 'id' : file_id})
print( 'Downloaded content "{}"'.format(downloaded.GetContentString()))


​还是要通过id来进行调用。毕竟这个调用只会发生在内部情况下,可以理解。 

到了小结时间了
昨天和今天在做CBIR的实验,得到目前这个结果,应该说能够解决一些问题,特别是模型训练这个方面的问题了,还是有很多收获的,当然主要的收获还是在代码里面:
1、agumenntation可以提高质量,fit_generate也是有技巧的(step_per_epoch),更不要说在数据集准备这块前期积累的技巧;
2、transform是可以用来解决垂直领域问题的,下一步就是需要验证;
3、研究问题的时候,首先是不能失去信心,然后就是要总结方法。


 




附件列表

     

    目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
    目录
    相关文章
    |
    6月前
    |
    算法 异构计算
    自研分布式训练框架EPL问题之帮助加速Bert Large模型的训练如何解决
    自研分布式训练框架EPL问题之帮助加速Bert Large模型的训练如何解决
    |
    9月前
    |
    物联网 网络架构
    PHATGOOSE:使用LoRA Experts创建低成本混合专家模型实现零样本泛化
    这篇2月的新论文介绍了Post-Hoc Adaptive Tokenwise Gating Over an Ocean of Specialized Experts (PHATGOOSE),这是一种通过利用一组专门的PEFT模块(如LoRA)实现零样本泛化的新方法
    102 0
    |
    9月前
    |
    机器学习/深度学习 存储 数据可视化
    【AAAI2024】M2SD:通过特征空间预构建策略重塑小样本类增量学习
    小样本类增量学习代表了机器学习领域中一个高度挑战性的议题,其核心目标在于能够在仅有限的数据支持下识别新类别,同时保留对已学习类别的认知,而无须重新训练整个模型。这一目标在模型需适应新类别的同时使用有限训练数据的情况下尤为艰巨。针对上述挑战,我们提出了一种创新性策略,称为多重混合自蒸馏。旨在为类增量学习阶段准备一个具有高度可扩展性和包容性的特征空间。
    |
    9月前
    |
    人工智能
    【Mixup】探索数据增强技术:深入了解Mixup操作
    【Mixup】探索数据增强技术:深入了解Mixup操作
    753 0
    |
    9月前
    |
    机器学习/深度学习 负载均衡 PyTorch
    PyTorch分布式训练:加速大规模数据集的处理
    【4月更文挑战第18天】PyTorch分布式训练加速大规模数据集处理,通过数据并行和模型并行提升训练效率。`torch.distributed`提供底层IPC与同步,适合定制化需求;`DistributedDataParallel`则简化并行过程。实际应用注意数据划分、通信开销、负载均衡及错误处理。借助PyTorch分布式工具,可高效应对深度学习的计算挑战,未来潜力无限。
    |
    6月前
    |
    机器学习/深度学习 PyTorch 数据处理
    数据增强与 DataLoader:提升模型泛化能力的策略
    【8月更文第29天】在深度学习中,数据的质量和数量对于模型的性能至关重要。数据增强是一种常用的技术,它通过对原始数据进行变换(如旋转、缩放、裁剪等)来生成额外的训练样本,从而增加训练集的多样性和规模。这有助于提高模型的泛化能力,减少过拟合的风险。同时,`DataLoader` 是 PyTorch 中一个强大的工具,可以有效地加载和预处理数据,并支持并行读取数据,这对于加速训练过程非常有帮助。
    663 1
    |
    9月前
    |
    存储 机器学习/深度学习 人工智能
    基于Megatron-Core的稀疏大模型训练工具:阿里云MoE大模型最佳实践
    随着大模型技术的不断发展,模型结构和参数量级快速演化。大模型技术的应用层出不穷。大模型展现惊人效果,但训练和推理成本高,一直是巨大挑战。模型稀疏化能降低计算和存储消耗。近期以Mixtral为代表的MoE(多专家混合)大模型证明了稀疏MoE技术能大幅降低计算量、提升推理速度,模型效果甚至超过同规模稠密模型。阿里云PAI和NVIDIA团队深入合作,基于Megatron-Core MoE框架,解决了MoE大模型训练落地时会遇到的可拓展性、易用性、功能性以及收敛精度等核心问题,在下游任务上取得了很好的模型效果。
    |
    存储 缓存 计算机视觉
    ECCV 2022 | 无需下游训练,Tip-Adapter大幅提升CLIP图像分类准确率
    ECCV 2022 | 无需下游训练,Tip-Adapter大幅提升CLIP图像分类准确率
    302 0
    |
    1月前
    |
    机器学习/深度学习 人工智能 计算机视觉
    NeurIPS 2024:无需训练,一个框架搞定开放式目标检测、实例分割
    在NeurIPS 2024会议上,论文提出了一种名为VL-SAM的框架,旨在解决开放式目标检测和实例分割任务。该框架结合了视觉语言模型(VLM)和Segment-Anything模型(SAM),利用注意力图作为提示,在无需额外训练的情况下实现未知物体的检测与分割。实验结果显示,VL-SAM在长尾实例分割数据集(LVIS)和角落情况目标检测数据集(CODA)上均表现出色,展示了其在现实世界应用中的潜力。然而,注意力图质量和计算复杂性仍是潜在挑战。
    61 19
    YOLOv5-Face | 原理超精细讲解、训练步骤还原、C++边缘部署(就要这么学!!!)(二)
    YOLOv5-Face | 原理超精细讲解、训练步骤还原、C++边缘部署(就要这么学!!!)(二)
    637 0

    热门文章

    最新文章

    相关实验场景

    更多