daisyjojo commited on
Commit
43bfed9
1 Parent(s): 98463c1

Upload deeprx_b3.py

Browse files
Files changed (1) hide show
  1. deeprx_b3.py +95 -0
deeprx_b3.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #3 resnet block version
2
+ import tensorflow as tf
3
+ import scipy.io as sio
4
+ import numpy as np
5
+ from sklearn.model_selection import train_test_split
6
+ from tensorflow_addons.optimizers import AdamW #pip install tensorflow_addons (Need to match tf version)
7
+
8
+ def residual_block(x, filters, dilation_rate,kernel_size=3):
9
+ # Shortcut分支
10
+ shortcut = x
11
+
12
+ # 第一个卷积层
13
+ x = tf.keras.layers.BatchNormalization()(x)
14
+ x = tf.keras.layers.ReLU()(x)
15
+ x = tf.keras.layers.SeparableConv2D(filters, kernel_size, depth_multiplier=2,dilation_rate=dilation_rate, padding='same')(x)
16
+ x = tf.keras.layers.Conv2D(filters, 1, padding='same')(x)
17
+ x = tf.keras.layers.BatchNormalization()(x)
18
+
19
+ # 第二个卷积层
20
+ x = tf.keras.layers.ReLU()(x)
21
+ x = tf.keras.layers.SeparableConv2D(filters, kernel_size,depth_multiplier=2, dilation_rate=dilation_rate, padding='same')(x)
22
+ x = tf.keras.layers.Conv2D(filters, 1, padding='same')(x)
23
+
24
+ # 如果维度不匹配,对shortcut进行适当的变换
25
+ if shortcut.shape[-1] != filters:
26
+ shortcut = tf.keras.layers.Conv2D(filters, 1, padding='same')(shortcut)
27
+
28
+ # 相加操作
29
+ x = tf.keras.layers.Add()([x, shortcut])
30
+ x = tf.keras.layers.ReLU()(x)
31
+
32
+ return x
33
+
34
+ def build_resnet(input_shape):
35
+ inputs = tf.keras.layers.Input(shape=input_shape)
36
+
37
+ # 初始卷积层
38
+ x = tf.keras.layers.Conv2D(64,3, strides=1, padding='same')(inputs)
39
+ x = tf.keras.layers.BatchNormalization()(x)
40
+ x = tf.keras.layers.ReLU()(x)
41
+
42
+ # 堆叠ResNet块
43
+ x = residual_block(x, filters=128, dilation_rate=(2,3))
44
+ x = residual_block(x, filters=448, dilation_rate=(3,6))
45
+ x = residual_block(x, filters=128, dilation_rate=(2,3))
46
+
47
+ outputs = tf.keras.layers.Conv2D(Morder, 3, strides=1, padding='same')(x)
48
+ outputs = tf.keras.activations.sigmoid(outputs)
49
+
50
+ model = tf.keras.Model(inputs, outputs)
51
+ return model
52
+
53
+ def load_data(m):
54
+ data_inputs = []
55
+ data_labels = []
56
+ for n in range(1,m+1):
57
+ input_data = sio.loadmat(f"SNR{n}_input.mat")["input_save"]
58
+ label_data = sio.loadmat(f"SNR{n}_label.mat")["label_save"]
59
+ input_data = np.transpose(input_data, (3,0,1,2))
60
+ label_data = np.transpose(label_data, (3,0,1,2))
61
+ data_inputs.append(input_data)
62
+ data_labels.append(label_data)
63
+ data_inputs = np.concatenate(data_inputs)
64
+ data_labels = np.concatenate(data_labels)
65
+ return data_inputs, data_labels
66
+ # 定义输入形状和类别数量
67
+ start = time.time()
68
+ input_shape = (312, 14, 6)
69
+ Morder = 4 # 16QAM
70
+ SNR_number = 10
71
+
72
+ # 创建ResNet模型
73
+ resnet_model = build_resnet(input_shape)
74
+ # 打印模型概要
75
+ resnet_model.summary()
76
+ ##################################################################
77
+
78
+ # 定义AdamW优化器,并设置学习率为0.01
79
+ #optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
80
+ optimizer = AdamW(learning_rate=0.01, weight_decay=1e-4)
81
+ # tensorboard
82
+ log_dir = "./log"
83
+ tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir =log_dir)
84
+ # 编译模型
85
+ resnet_model.compile(optimizer=optimizer, loss='binary_crossentropy',callbacks=[tensorboard_callback])
86
+ # read data
87
+ X_data,y_data = load_data(SNR_number)
88
+ print(X_data.shape,y_data.shape)
89
+ X_train, X_val, y_train, y_val = train_test_split(X_data, y_data, test_size=0.3, random_state=42)
90
+ # 训练模型
91
+ resnet_model.fit(X_train, y_train, epochs=10, batch_size=20, validation_data=(X_val, y_val))
92
+ # 保存
93
+ resnet_model.save("deeprx.h5")
94
+ endt = time.time()
95
+ print(endt-start)