ZHUyuan81 2022-09-28 16:47 采纳率: 50%
浏览 795
已结题

我正在运行卷积神经网络时,显示报错module ‘tensorflow.compat.v2‘ has no attribute ‘__internal__‘说是keras的问题又出现

环境
tensorflow 2.0.0
keras 2.10.0
cuda 11.2 win10(我的电脑是win11,只有win10版,有博主说不影响,我就装了)
python 3.6
用的spyter

我正在运行卷积神经网络时,显示报错module ‘tensorflow.compat.v2‘ has no attribute ‘__internal__‘

img


按照博主方法,说是keras的问题,我在图中84行前面加上tensorflow.
出现报错为

img


cannot import name 'np_utils'
请问是哪里出了问题

附我的完整代码

# -*- coding: utf-8 -*-
"""
第05讲 Keras卷积神经网络识别CIFAR-10图像
CIFAR-10数据集介绍
http://www.cs.toronto.edu/~kriz/cifar.html
"""

# Import Library,数据准备,标准化
import numpy as np
import os
os.environ["TF_KERAS"] = '1'
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import cv2
import tensorflow as tf

os.environ['CUDA_VISIBLE_DEVICES'] = '0'  # 0代表使用第一块GPU显卡
tf.config.set_soft_device_placement = False # TensorFlow 自动选择一个现有且受支持的设备来运行操作,以避免指定的设备不存在
tf.config.experimental.set_memory_growth = True # 仅在需要时申请显存空间
gpus = tf.config.experimental.list_physical_devices('GPU') # 获取全部的GPU显卡
print("gpus:", gpus)
 
if gpus:
    # gpus[0] 代表设置第一块显卡的配置,代表建立了显存大小为1GB的“虚拟GPU”
    tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=4096)])
    #logical_gpus = tf.config.experimental.list_logical_devices('GPU')
    #print(len(gpus), len(logical_gpus), 'Logical gpus')
    

# 随机种子
np.random.seed(10)

train_path = r'C:\train'

# 制作标签字典
label_dict = {i: j for i, j in enumerate(os.listdir(train_path))}

# 制作训练集和测试集
train_imgs = []
train_labels = []
test_imgs = []
test_labels = []

train_normalize_imgs = []
test_normalize_imgs = []

train_num = 10000       # 使用训练的图片数量
train_proportion = 0.9  # 训练集和测试集划分的比例
# 获取训练数据和训练标签
for label, path in enumerate(os.listdir(train_path)):
    images = os.listdir(os.path.join(train_path, path))
    images = images[:train_num]
    for index, img in enumerate(images):
        print(os.path.join(train_path,path,img))# 读取图片路径
        img = cv2.imread(os.path.join(train_path, path, img))
        print(img)
        img = cv2.resize(img, (100, 100), interpolation=cv2.INTER_AREA)
        # 判断训练数量,按训练比例分成训练集和测试集
        if index < train_num * train_proportion:
            train_labels.append(label)
            train_imgs.append(img)
            train_normalize_imgs.append(img.astype('float64'))
        else:
            test_labels.append(label)
            test_imgs.append(img)
            test_normalize_imgs.append(img.astype('float64'))


# 转换为tensorflow比较好处理的numpy数组
train_imgs = np.array(train_imgs)
train_labels = np.array(train_labels) 
test_imgs = np.array(test_imgs)
test_labels = np.array(test_labels)

train_normalize_imgs = np.array(train_normalize_imgs)
test_normalize_imgs = np.array(test_normalize_imgs)

# 输出形状
print("train data:", 'images:', train_imgs.shape, " labels:", train_labels.shape)
print("test  data:", 'images:', test_imgs.shape, " labels:", test_labels.shape)


# 转为one-hot编码
from tensorflow.keras.utils import np_utils

train_labels_OneHot = np_utils.to_categorical(train_labels)
test_labels_OneHot = np_utils.to_categorical(test_labels)


# 建立建立3次卷积神经网络模型

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense

model = Sequential()

# 卷积层1与池化层1
model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=(100, 100, 3),
                 activation='relu', padding='same'))

model.add(Conv2D(filters=32, kernel_size=(3, 3),
                 activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# 卷积层2与池化层2
model.add(Conv2D(filters=64, kernel_size=(3, 3),
                 activation='relu', padding='same'))
model.add(Conv2D(filters=64, kernel_size=(3, 3),
                 activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# 卷积层3与池化层3

model.add(Conv2D(filters=128, kernel_size=(3, 3),
                 activation='relu', padding='same'))
model.add(Conv2D(filters=128, kernel_size=(3, 3),
                 activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# 建立神经网络(平坦层、隐藏层、输出层)
model.add(Flatten())
model.add(Dropout(0.15))
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.15))
model.add(Dense(500, activation='relu'))
model.add(Dropout(0.15))
model.add(Dense(2, activation='softmax'))

# 输出模型列表,需要调整近1000万的参数
print(model.summary())

# 训练模型
epochs = 20  # 训练次数
model.compile(loss='categorical_crossentropy', optimizer='adam',
              metrics=['accuracy'])

train_history = model.fit(train_normalize_imgs, train_labels_OneHot,
                          validation_split=0.0005,shuffle=True,
                          epochs=epochs, batch_size=256)

# 评估模型的准确率
import matplotlib.pyplot as plt


def show_train_history(train_history, train_acc, test_acc):
    plt.plot(train_history.history[train_acc])
    plt.plot(train_history.history[test_acc])
    plt.title('Train History')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()


show_train_history(train_history, 'accuracy', 'val_accuracy')
show_train_history(train_history, 'loss', 'val_loss')

scores = model.evaluate(test_normalize_imgs,
                        test_labels_OneHot, verbose=0)


print("scores:", scores)

# 进行预测
prediction = model.predict_classes(test_normalize_imgs)
from collections import Counter
result = Counter(prediction[1000:])
print('cat 识别数量:' , result)
result = Counter(prediction[1000:])
print('dog 识别数量:' , result)

"""
def plot_images_labels_prediction(images, labels, prediction,
                                  idx, num=10):
    fig = plt.gcf()
    fig.set_size_inches(12, 14)
    if num > 25: num = 25
    for i in range(0, num):
        ax = plt.subplot(5, 5, 1 + i)
        ax.imshow(images[idx], cmap='binary')

        title = str(i) + ',' + label_dict[labels[i]]
        if len(prediction) > 0:
            title += '=>' + label_dict[prediction[i]]
        ax.set_title(title, fontsize=10)
        ax.set_xticks([]);
        ax.set_yticks([])
        idx += 1
    plt.show()


plot_images_labels_prediction(test_normalize_imgs, test_labels,
                              prediction, 0, 10)
"""
# 查看预测概率
Predicted_Probability = model.predict(test_normalize_imgs)
print("Predicted_Probability:", Predicted_Probability)


def show_Predicted_Probability(X_img, Predicted_Probability, i):
    plt.figure(figsize=(2, 2))
    plt.imshow(np.reshape(test_imgs[i], (100, 100, 3)))
    plt.show()
    for j in range(2):
        print(label_dict[j] + ' Probability:%1.9f' % (Predicted_Probability[i][j]))


show_Predicted_Probability(test_imgs, Predicted_Probability, 0)



# 加载要预测的图片
# Load images
img_names = ['test_cat.jpg', 'test_dog.jpg']

imgs = []
for img_name in img_names:
    img = cv2.imread(img_name)
    img = cv2.resize(img, (100, 100), interpolation=cv2.INTER_AREA)
    img = img.astype('float32')
    imgs.append(img)

imgs = np.array(imgs) / 255

predictions = model.predict_classes(imgs)
print(predictions)


# 保存模型和json文件
open('Keras_Cifar_CNN_Conv3_architecture.json', 'w').write(model.to_json())
model.save('CNN.h5')
model.save_weights('Keras_Cifar_CNN_Conv3_weights.h5', overwrite=True)

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-28 17:12
    关注
    评论

报告相同问题?

问题事件

  • 系统已结题 10月6日
  • 赞助了问题酬金1元 9月28日
  • 创建了问题 9月28日

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了