《python+深度学习》中的代码【1】

《python+深度学习》第二章事例中的代码如下

#$1 加载Keras 中的Mnist数据集
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
#看一下训练数据
print("This is train data:")
print("train_images.shape:",train_images.shape)
print("len(train_labels):",len(train_labels))
print("train_labels:",train_labels)
#看一下测试数据
print("this is test data:")
print("test_images.shape:",test_images.shape)
print("len(test_labels):",len(test_labels))
print("test_labels:",test_labels)
#本段代码用于忽略AVX2警告
import os 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#$2 网络架构
from keras import models
from keras import layers

#把网络分成密集连接(全连接)
network = models.Sequential()
network.add(layers.Dense(512, activation='relu',input_shape =(28*28,)))
#第二层是一个10路softmax层,他将返回一个由10个概率值组曾的数组。
network.add(layers.Dense(10,activation='softmax'))

#$3 编译步骤
#包
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个基于 Python+PyQt+TensorFlow 的图像转素描的代码示例,供你参考: ```python import sys from PyQt5.QtCore import Qt from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QLabel, QFileDialog import tensorflow as tf import numpy as np class SketchGenerator: def __init__(self, model_path): self.model = tf.keras.models.load_model(model_path) def generate_sketch(self, image_path): # Load the input image image = tf.keras.preprocessing.image.load_img(image_path) image = tf.keras.preprocessing.image.img_to_array(image) image = np.expand_dims(image, axis=0) # Generate the sketch sketch = self.model.predict(image) # Rescale the sketch to [0, 255] sketch = (sketch * 255).astype(np.uint8) return sketch.squeeze() class MainWindow(QMainWindow): def __init__(self): super().__init__() # Create the sketch generator self.sketch_generator = SketchGenerator('sketch_generator.h5') # Create the menu bar file_menu = self.menuBar().addMenu('File') # Create the Open action open_action = QAction('Open', self) open_action.setShortcut('Ctrl+O') open_action.triggered.connect(self.open_image) file_menu.addAction(open_action) # Create the Save action save_action = QAction('Save', self) save_action.setShortcut('Ctrl+S') save_action.triggered.connect(self.save_sketch) file_menu.addAction(save_action) # Create the image label self.image_label = QLabel(self) self.image_label.setAlignment(Qt.AlignCenter) self.setCentralWidget(self.image_label) # Set the window properties self.setWindowTitle('Sketch Generator') self.setMinimumSize(640, 480) def open_image(self): # Open the file dialog file_path, _ = QFileDialog.getOpenFileName(self, 'Open Image', '', 'Images (*.png *.jpg *.jpeg *.bmp)') if file_path: # Load the image image = QImage(file_path) # Show the image self.image_label.setPixmap(QPixmap.fromImage(image)) # Generate and show the sketch sketch = self.sketch_generator.generate_sketch(file_path) sketch_image = QImage(sketch.data, sketch.shape[1], sketch.shape[0], QImage.Format_Grayscale8) self.image_label.setPixmap(QPixmap.fromImage(sketch_image)) def save_sketch(self): # Get the current pixmap pixmap = self.image_label.pixmap() if pixmap: # Open the file dialog file_path, _ = QFileDialog.getSaveFileName(self, 'Save Sketch', '', 'Images (*.png *.jpg *.jpeg *.bmp)') if file_path: # Save the pixmap pixmap.save(file_path) if __name__ == '__main__': # Create the application app = QApplication(sys.argv) # Create the main window window = MainWindow() window.show() # Run the event loop sys.exit(app.exec_()) ``` 在这个示例代码,我们首先定义了一个 `SketchGenerator` 类,用于加载深度学习模型并生成素描风格的图像。然后,我们创建了一个 `MainWindow` 类,用于显示图像和进行用户交互。在 `MainWindow` 类,我们创建了一个菜单栏,包含打开和保存文件的功能。当用户打开一个图像文件时,我们会在窗口显示该图像,并使用 `SketchGenerator` 类生成相应的素描风格图像。当用户保存素描风格图像时,我们会将当前的像素图保存到指定的文件。 请注意,这个示例代码使用的深度学习模型是一个预训练的模型,你需要使用你自己的数据集来训练一个模型。此外,你还需要安装 TensorFlow 和 PyQt5 库才能运行这个示例代码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值