QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1613|回复: 0
打印 上一主题 下一主题

2021美赛C题编程

[复制链接]
字体大小: 正常 放大
白山茶        

1

主题

1

听众

1

积分

升级  20%

该用户从未签到

群组数学建模美赛备战群组

跳转到指定楼层
1#
发表于 2022-2-13 11:24 手机频道 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
import osfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltimport tensorflow as tffrom tensorflow.keras import datasets, layers, modelsdef read_image(paths):    os.listdir(paths)    filelist = []    for root, dirs, files in os.walk(paths):        for file in files:            if os.path.splitext(file)[1] == ".jpg":                filelist.append(os.path.join(root, file))    return filelistdef im_xiangsu(paths):    for filename in paths:        try:            im = Image.open(filename)            newim = im.resize((128, 128))            newim.save('F:/CNN/test/' + filename[12:-4] + '.jpg')            print('图片' + filename[12:-4] + '.jpg' + '像素转化完成')        except OSError as e:            print(e.args)def im_array(paths):    M=[]    for filename in paths:        im=Image.open(filename)        im_L=im.convert("L")                #模式L        Core=im_L.getdata()        arr1=np.array(Core,dtype='float32')/255.0        list_img=arr1.tolist()        M.extend(list_img)    return Mpath_1='引号内为训练图集所在文件路径'filelist_1=read_image(path_1)M=[]M=im_array(filelist_1)dict_label={0:'亚洲大黄蜂'}train_images=np.array(M).reshape(len(filelist_1),128,128)label=[0]*len(filelist_1)train_lables=np.array(label)        #数据标签train_images = train_images[ ..., np.newaxis ]        #数据图片print(train_images.shape)#输出验证一下(400, 128, 128, 1)model = models.Sequential()model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 1)))#过滤器个数,卷积核尺寸,激活函数,输入形状model.add(layers.MaxPooling2D((2, 2)))#池化层model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.Flatten())#降维model.add(layers.Dense(64, activation='relu'))#全连接层model.add(layers.Dense(1, activation='softmax'))model.summary()  # 显示模型的架构model.compile(optimizer='adam',              loss='sparse_categorical_crossentropy',              metrics=['accuracy'])#epochs为训练多少轮、batch_size为每次训练多少个样本model.fit(train_images, train_lables, epochs=5)model.save('my_model.h5') #保存为h5模型#tf.keras.models.save_model(model,"F:\python\moxing\model")#这样是pb模型print("模型保存成功!")
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2024-4-27 08:38 , Processed in 0.473905 second(s), 49 queries .

回顶部