深度学习笔记

本文旨在记录一些在学习神经网络时用到的指令、下载地址以及知识点

Anaconda下载地址:Free Download | Anaconda

win11的cmd好像不能使用anaconda,需要找到Anaconda Prompt打开

Anaconda简单命令:
conda deactivate退出当前虚拟环境

conda creat -n 虚拟环境名字 python=3.7创建python版本为3.7的虚拟环境

conda activate 虚拟环境名字 进入虚拟环境,会发现前面有括号提示你进入的虚拟环境名字

conda info --envs / conda envs list 列出所有虚拟环境
numpy和matplotlib下载:
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
tensorflow CPU版本下载:(在带有numpy的虚拟环境中下载)
pip install tensorflow -i https://mirrors.aliyun.com/pipy/simple
tensorflow GPU版本下载:
pip install -i https://pypi.douban.com/simple tensorflow-gpu==2.2.0

tensorflow cpu不需要安装cuda和cudnn;tensorflow-gpu需要安装cuda和cudnn

tensorflow下载的时候对网络要求很高,下载失败看看有没有未关闭的clash等脚本

tensorflow GPU版本对应cuda,cudnn图:

下载TensorFlow后用Python引用TensorFlow时报错:

原因是protoc版本不对,这里重新安装protoc:

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple protobuf==3.19.0
mnist.npz下载:(mnist数据集是一个非常经典的小像素数据集)

链接: https://pan.baidu.com/s/16I4-89x8fHjbGGGThuZXGQ 提取码: 1234 

什么是正则化?

        正则化用来防止过拟合。在搭建神经网络时可以选择kernel_regularizer参数来选取哪一种正则化方法.

什么是Dropout?

        在训练时将随机一部分神经元暂时舍去,防止过拟合。神经网络使用时,神经元恢复。

什么是CBAPD?

        卷积神经网络的构建步骤:convolutional,batch normalization,activation,pooling,dropout.

卷积、批标准化,激活,池化,舍弃.

1*1卷积核为什么可以降维?

        处理的图片有几个通道,提取 feature map的卷积核就应该是多少维度.比如一个图片有256个通道,用64个256维度的1*1卷积核进行特征提取,最后得到64个通道的图片信息,比256少.

什么是优化器?

        优化器是在深度学习的反向传播过程中,指引损失函数(目标函数)的各个参数往正确的方向更新合适的大小,使得更新后的各个参数让目标函数不断逼近全局最小点。

什么是标准化,什么是批标准化?

标准化:使数据符合0均值,1为标准差的分布.

批标准化:对一小批数据(batch)进行标准化.

pandas用到的函数:
x_data = DataFrame(x_data,columns=['length1','width1','length2','width2'])

将数据集制成表格,column里面的参数是每一个列上的名字.

pd.set_option('display.unicode.east_asian_width',True)

设置列名对齐.

x_data['class']=y_data

加一列,列名为class,内容为y_data.

        特征过多会导致过拟合,特征过少会导致欠拟合。过拟合在验证集上效果不好,欠拟合在验证集和测试集上效果都不好。过拟合的解决方法是加大损失函数惩罚力度。

validation_freq表示多少epoch测试一次.

validation表示划分测试集的比率,在fit设置过后就不用提前划分测试集与数据集了.

用了softmax以后from_logits=False.

一个图像分类简单历程:
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import glob
import os

imgs_path = glob.glob('datasets/*/*.jpg')
all_labels_name = [img_p.split('/')[1] for img_p in imgs_path]
label_names = np.unique(all_labels_name)
label_to_index = dict((name,i) for i,name in enumerate(label_names))
index_to_label = dict((i,name) for name,i in label_to_index.items())
all_labels = [label_to_index.get(name) for name in all_labels_name]
random_index = np.random.permutation(len(imgs_path))
imgs_path = np.array(imgs_path)[random_index]
all_labels = np.array(all_labels)[random_index]
i = int(len(imgs_path)*0.8)
train_path = imgs_path[:i]
train_labels = all_labels[:i]
test_path = imgs_path[i:]
test_labels = all_labels[i:]
train_ds = tf.data.Dataset.from_tensor_slices((train_path,train_labels))
test_ds = tf.data.Dataset.from_tensor_slices((test_path,test_labels))

def load_img(path,label):
    image = tf.io.read_file(path)
    image = tf.image.decode_jpeg(image,channels = 3)
    image = tf.image.resize(image,[32,32])
    image = tf.cast(image,tf.float32)
    image = image/255
    return image,label

AUTOTUNE = tf.data.experimental.AUTOTUNE
train_ds = train_ds.map(load_img,num_parallel_calls=AUTOTUNE)
test_ds = test_ds.map(load_img,num_parallel_calls=AUTOTUNE)

BATCH_SIZE = 32
train_ds = train_ds.repeat().shuffle(100).batch(BATCH_SIZE)
test_ds = test_ds.batch(BATCH_SIZE)

train_count = len(train_path)
test_count = len(test_path)
steps_per_epoch = train_count//BATCH_SIZE
validation_steps = test_count//BATCH_SIZE

def create_model():
  model = tf.keras.Sequential([
      tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
      tf.keras.layers.MaxPooling2D((2, 2)),
      tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
      tf.keras.layers.MaxPooling2D((2, 2)),
      tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
      tf.keras.layers.Flatten(),
      tf.keras.layers.Dense(64, activation='relu'),
      tf.keras.layers.Dense(16)
  ])

  model.compile(optimizer='adam',
                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])

  return model

# Create and train a new model instance.
model = create_model()
model.fit(train_ds, epochs=5,
           steps_per_epoch=steps_per_epoch,
           validation_data=test_ds,
           validation_steps=validation_steps)

# Save the entire model as a SavedModel.
model.save('saved_model/my_model')

搭建网络八股:
class IrisModel(Model):
    def __init__(self):
        super(IrisModel,self).__init__()
        self.d1 = Dense(3,activation='sigmoid',kernel_regularizer=tf.keras.regularizers.l2()) 
    def call(self,x):
        y = self.d1(x)
        return y
matplotlib根据路径显示图片:
x=plt.imread(train_path[0])
plt.imshow(x)
plt.show()

数据增强方法:

制作自己的数据集将其返回成三维numpy数组的一维数组形式:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import glob

def get_dataset2():
    imgs_path = glob.glob('datasets/*/*.jpg')
    all_labels_name = [img_p.split('\\')[1] for img_p in imgs_path]
    label_names = np.unique(all_labels_name)
    label_to_index = dict((name,i) for i,name in enumerate(label_names))
    index_to_label = dict((i,name) for name,i in label_to_index.items())
    all_labels = [label_to_index.get(name) for name in all_labels_name]
    random_index = np.random.permutation(len(imgs_path))
    imgs_path = np.array(imgs_path)[random_index]
    all_labels = np.array(all_labels)[random_index]
    i = int(len(imgs_path)*0.8)
    train_path = imgs_path[:i]
    train_labels = all_labels[:i]
    test_path = imgs_path[i:]
    test_labels = all_labels[i:]
    
    BATCH_SIZE = 32
    
    train_count = len(train_path)
    test_count = len(test_path)
    steps_per_epoch = train_count//BATCH_SIZE
    validation_steps = test_count//BATCH_SIZE
    
    # 加载训练图像数据
    train_images = []
    for path in train_path:
        image = load_img(path)
        train_images.append(image)
    train_images = np.array(train_images)
    
    # 加载测试图像数据
    test_images = []
    for path in test_path:
        image = load_img(path)
        test_images.append(image)
    test_images = np.array(test_images)

    return train_images,test_images,train_labels,test_labels,steps_per_epoch,validation_steps

def load_img(path):
    image = tf.io.read_file(path)
    image = tf.image.decode_jpeg(image,channels = 3)
    image = tf.image.resize(image,[64,64])
    image = tf.cast(image,tf.int8)
    image = image/255
    return image
制作自己的数据集并将其返回成Tensorflow切片模式:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import glob

def get_dataset():
    imgs_path = glob.glob('datasets/*/*.jpg')
    all_labels_name = [img_p.split('\\')[1] for img_p in imgs_path]
    label_names = np.unique(all_labels_name)
    label_to_index = dict((name,i) for i,name in enumerate(label_names))
    index_to_label = dict((i,name) for name,i in label_to_index.items())
    all_labels = [label_to_index.get(name) for name in all_labels_name]
    random_index = np.random.permutation(len(imgs_path))
    imgs_path = np.array(imgs_path)[random_index]
    all_labels = np.array(all_labels)[random_index]
    i = int(len(imgs_path)*0.8)
    train_path = imgs_path[:i]
    train_labels = all_labels[:i]
    test_path = imgs_path[i:]
    test_labels = all_labels[i:]
    train_ds = tf.data.Dataset.from_tensor_slices((train_path,train_labels))
    test_ds = tf.data.Dataset.from_tensor_slices((test_path,test_labels))
    AUTOTUNE = tf.data.experimental.AUTOTUNE
    train_ds = train_ds.map(load_img,num_parallel_calls=AUTOTUNE)
    test_ds = test_ds.map(load_img,num_parallel_calls=AUTOTUNE)
    
    BATCH_SIZE = 32
    train_ds = train_ds.repeat().shuffle(100).batch(BATCH_SIZE)
    test_ds = test_ds.batch(BATCH_SIZE)
    
    train_count = len(train_path)
    test_count = len(test_path)
    steps_per_epoch = train_count//BATCH_SIZE
    validation_steps = test_count//BATCH_SIZE
    return train_ds,test_ds,steps_per_epoch,validation_steps

def load_img(path,label):
    image = tf.io.read_file(path)
    image = tf.image.decode_jpeg(image,channels = 3)
    image = tf.image.resize(image,[64,64])
    image = tf.cast(image,tf.float32)
    image = image/255
    return image,label


基于MobileNetV2的迁移学习:
import tensorflow as tf
from keras import layers, models
from keras.applications import MobileNetV2
import numpy as np
import matplotlib.pyplot as plt
import glob

imgs_path = glob.glob('datasets/*/*.jpg')
all_labels_name = [img_p.split('\\')[1] for img_p in imgs_path]
label_names = np.unique(all_labels_name)
label_to_index = dict((name,i) for i,name in enumerate(label_names))
index_to_label = dict((i,name) for name,i in label_to_index.items())
all_labels = [label_to_index.get(name) for name in all_labels_name]
random_index = np.random.permutation(len(imgs_path))
imgs_path = np.array(imgs_path)[random_index]
all_labels = np.array(all_labels)[random_index]
i = int(len(imgs_path)*0.8)
train_path = imgs_path[:i]
train_labels = all_labels[:i]
test_path = imgs_path[i:]
test_labels = all_labels[i:]
train_ds = tf.data.Dataset.from_tensor_slices((train_path,train_labels))
test_ds = tf.data.Dataset.from_tensor_slices((test_path,test_labels))

def load_img(path,label):
    image = tf.io.read_file(path)
    image = tf.image.decode_jpeg(image,channels = 3)
    image = tf.image.resize(image,[64,64])
    image = tf.cast(image,tf.float32)
    image = image/255
    return image,label


AUTOTUNE = tf.data.experimental.AUTOTUNE
train_ds = train_ds.map(load_img,num_parallel_calls=AUTOTUNE)
test_ds = test_ds.map(load_img,num_parallel_calls=AUTOTUNE)
 
BATCH_SIZE = 32
train_ds = train_ds.repeat().shuffle(100).batch(BATCH_SIZE)
test_ds = test_ds.batch(BATCH_SIZE)
 
train_count = len(train_path)
test_count = len(test_path)
steps_per_epoch = train_count//BATCH_SIZE
validation_steps = test_count//BATCH_SIZE

# 基于MobileNetV2的预训练模型
base_model = MobileNetV2(input_shape=(64, 64, 3), include_top=False, weights='imagenet')

# 冻结预训练模型的参数
base_model.trainable = False

# 构建模型
model = models.Sequential([
base_model,
layers.GlobalAveragePooling2D(), # 使用全局平均池化层
# 可以根据需要添加额外的层
layers.Dense(128, activation='relu'),
layers.Dropout(0.2), # 添加Dropout防止过拟合

# 分类层
layers.Dense(16, activation='softmax')
])

model.compile(optimizer='adam',
                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])

model.summary()
def run_model(epoch):
    history = model.fit(train_ds, epochs=epoch,
            steps_per_epoch=steps_per_epoch,
            validation_data=test_ds,
            validation_steps=validation_steps)

    acc = history.history['sparse_categorical_accuracy']
    val_acc = history.history['val_sparse_categorical_accuracy']
    loss  = history.history['loss']
    val_loss = history.history['val_loss']

    plt.subplot(1,2,1)
    plt.plot(acc,label = 'Training Accuracy')
    plt.plot(val_acc,label = 'Validation Accuracy')
    plt.title('Training and Validation Accuracy')
    plt.legend()

    plt.subplot(1,2,2)
    plt.plot(loss,label = 'Training Loss')
    plt.plot(val_loss,label = 'Validation Loss')
    plt.title('Training and Validation Loss')
    plt.legend()
    plt.show()

run_model(10)
以class的形式定义一个CNN模型:
class MyModel(keras.Model):
    def __init__(self):
        super().__init__()
        self.Conv2D1 = keras.layers.Conv2D(32,(3,3),activation='relu')
        self.Pooling = keras.layers.MaxPooling2D((2,2))
        self.Conv2D2 = keras.layers.Conv2D(64,(3,3),activation='relu')
        self.Conv2D3 = keras.layers.Conv2D(64,(3,3),activation='relu') # Rename to Conv2D3
        self.flatten = keras.layers.Flatten()
        self.dense1 = keras.layers.Dense(64,activation='relu')
        self.dense2 = keras.layers.Dense(16,activation = 'softmax')

    def call(self,inputs):
        x = self.Conv2D1(inputs)
        x = self.Pooling(x)
        x = self.Conv2D2(x)
        x = self.Pooling(x)
        x = self.Conv2D3(x) # Changed from Conv2D2 to Conv2D3
        x = self.flatten(x)
        x = self.dense1(x)
        x = self.dense2(x) # Changed from dense to dense2
        return x
    
model = MyModel()

model.compile(optimizer = 'adam',
              loss = keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=keras.metrics.SparseCategoricalAccuracy())

inputshape = (32, 32, 32, 3) # Fix the input shape
model.build(inputshape)
model.summary()

model.fit(train_ds, epochs=20,
           steps_per_epoch=steps_per_epoch,
           validation_data=test_ds,
           validation_steps=validation_steps)

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值