多层感应神经网络、卷积神经网络预测图片复杂度数值

应用场景:
1:将固定区域内拍摄视频复杂度,密度等指标数值化

2:预测雾霾指数

3:预测绿潮、赤潮指数

4:预测太阳板灰尘堆积程度
在这里插入图片描述

#多层感应器神经网络模型
# 0. 调用要使用的包
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
width = 20
height = 20



def generate_dataset(samples):
    ds_x = []
    ds_y = []
    for it in range(samples):
        num_pt = np.random.randint(0, width * height)   #产生一幅图像的像素点个数  在0到16*16之间
        img = generate_image(num_pt)                    # 产生一副图像
        ds_y.append(num_pt)            #标签    也就是一副图像像素值为1 的个数
        ds_x.append(img)               #一副图像的数值              
    return np.array(ds_x), np.array(ds_y).reshape(samples, 1)   #展开一副图像与标签



def generate_image(points):
    img = np.zeros((width, height))        #一副图像全为0      16行 *16 列
    pts = np.random.random((points, 2))     #  points行 * 2列 的随机浮点数,浮点范围(0到1)
    for ipt in pts:
        img[int(ipt[0] * width), int(ipt[1] * height)] = 1         #一副图像中 像素为1 的点
    return img.reshape(width, height, 1)




# 1. 生成数据集
x_train, y_train = generate_dataset(1500)
x_val, y_val = generate_dataset(300)
x_test, y_test = generate_dataset(100)

x_train_1d = x_train.reshape(x_train.shape[0], width*height)
x_val_1d = x_val.reshape(x_val.shape[0], width*height)
x_test_1d = x_test.reshape(x_test.shape[0], width*height)


# 2. 搭建模型
model = Sequential()
model.add(Dense(256, activation='relu', input_dim = width*height))
model.add(Dense(256, activation='relu'))
model.add(Dense(256))
model.add(Dense(1))


# 3. 设置模型训练过程
model.compile(loss='mse', optimizer='adam')

# 4. 训练模型
hist = model.fit(x_train_1d, y_train, batch_size=32, epochs=1000, validation_data=(x_val_1d, y_val))


# 5. 查看训练过程
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.ylim(0.0, 1000.0)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

# 6. 评价模型
score = model.evaluate(x_test_1d, y_test, batch_size=32)
print(score)

# 7. 调用模型
yhat_test = model.predict(x_test_1d, batch_size=32)

%matplotlib inline
import matplotlib.pyplot as plt
plt_row = 5
plt_col = 5
plt.rcParams["figure.figsize"] = (10,10)     #字体   样式
f, axarr = plt.subplots(plt_row, plt_col)
for i in range(plt_row*plt_col):
    sub_plt = axarr[i//plt_row, i%plt_col]    #可以做成一个矩阵 (01 02 03 04 05)(11 12 13 14 15 )
    sub_plt.axis('off')                        #关闭坐标轴
    sub_plt.imshow(x_test[i].reshape(width, height))    #展示测试集中 0 到 25 张图片
    sub_plt.set_title('R %d P %.1f' % (y_test[i][0], yhat_test[i][0]))   #标题
plt.show()


#卷积神经网络模型
#  调用要使用的包
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
width = 20
height = 20


def generate_dataset(samples):
    ds_x = []
    ds_y = []
    for it in range(samples):
        num_pt = np.random.randint(0, width * height)
        img = generate_image(num_pt)
        ds_y.append(num_pt)
        ds_x.append(img)
    return np.array(ds_x), np.array(ds_y).reshape(samples, 1)



def generate_image(points):
    img = np.zeros((width, height))
    pts = np.random.random((points, 2))
    for ipt in pts:
        img[int(ipt[0] * width), int(ipt[1] * height)] = 1
    return img.reshape(width, height, 1)


# 1. 生成数据集
x_train, y_train = generate_dataset(1500)
x_val, y_val = generate_dataset(300)
x_test, y_test = generate_dataset(100)


# 2. 搭建模型
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(width, height, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(1))

# 3. 设置模型训练过程
model.compile(loss='mse', optimizer='adam')
# 4. 训练模型
hist = model.fit(x_train, y_train, batch_size=32, epochs=1000, validation_data=(x_val, y_val))

# 5. 查看训练过程
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.ylim(0.0, 1000.0)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

# 6. 评价模型
score = model.evaluate(x_test, y_test, batch_size=32)
print(score)
# 7. 调用模型
yhat_test = model.predict(x_test, batch_size=32)


%matplotlib inline
import matplotlib.pyplot as plt
plt_row = 5
plt_col = 5
plt.rcParams["figure.figsize"] = (10,10)
f, axarr = plt.subplots(plt_row, plt_col)
for i in range(plt_row*plt_col):
    sub_plt = axarr[i//plt_row, i%plt_col]
    sub_plt.axis('off')
    sub_plt.imshow(x_test[i].reshape(width, height))
    sub_plt.set_title('R %d P %.1f' % (y_test[i][0], yhat_test[i][0]))
plt.show()


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值