简体   繁体   English

Python为神经网络重塑RGB图像

[英]Python reshape RGB images for neural network

I'm working of facial expressions recognition, I have a dataset of images on grayscale mode stored in a csv file like this : 我正在进行面部表情识别,我将一个灰度模式下的图像数据集存储在csv文件中,如下所示:

Emotion  |  Pixels  |  Usage

Where Emotion is the actual emotion of the image ( Pixels column), and Usage is either : Training, Validation or Test. 情感”是图像的实际情感(“ 像素”列),“ 用法”是:训练,验证或测试。

All images are 48 x 48. Pixels stored in the csv file are reshaped in an array (1, 2304) (one line) like this : 所有图像均为48 x48。将csv文件中存储的像素调整为数组(1、2304)(一行)的形状,如下所示:

169 140 118 115 129 129 111 107 95 115 114 119 138 136 111 119 144 157 165 167 166 171 172 177 182 186 191 187 187 191 190 191 186 177 158 143 144 142 139 122 97 98 110 149 217 222 220 221 151 131 116 125 127 111 106 98 113 108 108 133 134 104 118 152 168 175 181 181 181 180 180 183 188 192 189 190 190 192 193 191 189 185 176 157 144 144 133 132 115 99 104 127 198 226 221 220 137 122 123 125 105 107 101 115 101 101 127 132 95 117 162 186 190 192 195 196 193 192 193 195 197 198 196 196 195 197 198 199 194 184 179 173 158 139 129 121 124 110 99 113 179 227 220 221 133 121 127 108 105 104 115 97 95 120 128 93 111 162 191 197 196 198 195 196 198 199 198 197 200 201 203 202 202 203 201 202 200 188 179 171 164 147 127 117 117 113 96 100 165 225 221 221 131 128 117 103 101 116 96 85 105 123 100 109 153 176 186 192 196 198 196 198 197 198 197 199 200 198 197 193 196 195 196 195 193 189 182 170 152 143 130 115 105 112 102 89 151 223 220 221 125 120 103 94 110 98 77 102 120 103 112 146 167 174 177 183 185 187 187 191 185 184 189 192 193 189 186 181 184 184 184 183 181 180 179 170 159 150 136 115 99 99 109 88 131 220 221 220 125 108 94 98 111 71 96 121 104 111 142 165 170 172 176 176 178 179 182 181 180 180 181 182 181 179 175 176 177 178 179 179 178 172 170 170 168 161 153 126 100 92 106 95 110 209 222 220 120 95 88 113 81 90 118 99 106 141 162 168 173 170 172 173 176 179 181 178 177 176 176 175 177 176 173 174 174 173 176 178 171 167 168 167 168 165 158 142 112 95 94 102 103 199 223 219 111 83 97 99 90 113 93 100 138 161 163 166 170 174 172 172 174 175 179 178 175 176 179 174 179 177 175 179 179 176 177 174 167 167 170 171 169 163 156 149 123 99 88 99 102 193 224 219 103 85 112 97 96 91 104 137 153 163 167 166 167 174 175 173 173 177 182 185 181 179 182 177 182 185 182 189 187 182 181 173 169 169 172 173 169 164 156 150 132 102 81 92 102 187 224 219 86 98 95 81 74 108 141 146 156 165 167 168 167 170 175 174 172 175 180 187 192 192 190 181 190 194 193 194 190 186 181 173 165 169 173 170 167 162 155 154 143 105 83 81 104 188 225 219 81 71 69 76 98 143 146 153 160 163 168 170 167 164 171 175 170 167 170 182 191 198 197 190 196 201 200 196 185 183 179 166 168 175 177 172 168 164 160 154 152 113 85 81 104 197 224 219 81 65 84 80 122 153 151 157 163 167 170 173 172 172 173 172 169 165 158 168 181 188 191 192 197 200 197 181 170 177 175 169 179 182 181 178 173 168 161 156 158 124 82 82 119 210 221 219 93 85 91 87 139 156 153 157 163 168 177 180 174 178 180 181 184 180 167 161 169 173 173 183 194 184 177 168 164 174 179 180 173 166 167 173 175 173 165 157 159 140 81 82 141 222 221 221 107 87 91 90 153 156 155 159 167 166 159 159 149 138 135 139 145 148 154 165 157 157 155 166 181 166 158 155 155 144 131 123 105 104 129 154 148 145 162 162 159 153 96 89 179 225 218 220 144 106 82 103 157 158 160 162 159 146 144 158 159 145 119 87 79 74 94 122 129 133 128 151 176 151 131 129 103 81 72 74 92 122 164 175 164 146 144 157 158 159 109 110 187 220 219 219 140 143 87 115 160 158 158 156 151 156 166 167 164 141 101 71 58 66 68 71 82 101 122 154 161 162 125 88 66 62 50 44 55 91 116 141 154 155 151 153 158 160 117 134 149 210 221 219 107 127 100 123 159 157 155 147 142 142 130 96 62 41 29 26 30 29 53 61 60 109 159 166 159 153 121 79 74 52 42 36 34 31 43 40 56 116 136 146 157 160 131 108 131 219 220 220 141 143 120 125 157 157 151 140 133 110 44 36 73 49 38 49 105 77 58 88 95 119 165 182 183 167 122 117 103 79 114 71 30 47 112 122 50 65 123 141 154 161 140 108 150 228 218 220 148 147 127 132 158 157 154 145 129 82 65 118 153 92 38 71 147 132 103 121 148 143 162 192 193 169 143 153 132 125 139 117 62 73 145 156 116 89 117 146 155 162 148 121 188 225 218 219 177 146 116 135 161 159 158 156 142 130 127 117 138 132 112 129 140 129 137 145 153 153 159 186 197 169 152 154 149 138 127 131 120 125 135 115 120 140 143 158 161 165 146 154 219 220 219 219 202 156 116 138 162 159 161 164 166 161 154 131 118 112 115 118 123 137 137 148 157 157 156 178 186 163 158 159 151 137 126 109 109 108 115 126 143 158 168 170 163 162 147 183 222 218 219 218 216 181 119 141 162 161 163 170 173 165 160 159 149 145 145 147 150 147 154 163 163 163 158 174 180 164 161 166 165 156 142 139 140 144 149 154 159 165 171 170 164 162 144 190 223 219 217 217 219 199 134 146 159 160 164 171 172 169 164 161 159 157 156 151 151 165 173 165 165 162 160 175 182 166 163 170 167 173 166 151 148 154 156 160 163 170 174 170 164 162 142 193 224 218 219 219 219 218 161 150 161 160 163 169 172 174 171 165 160 156 155 165 176 181 171 165 167 162 164 177 183 170 165 171 169 170 183 184 169 160 164 169 173 173 172 171 162 158 162 213 220 219 219 219 221 229 200 149 164 157 159 165 169 171 173 174 174 174 177 181 179 176 168 169 166 162 171 186 189 174 164 167 171 170 179 183 185 178 174 178 175 170 167 164 161 155 200 224 217 218 219 219 227 222 229 181 151 157 154 159 161 162 166 173 171 173 177 179 175 173 167 155 150 160 177 192 191 181 169 154 160 173 178 180 177 176 174 170 166 162 160 158 151 161 216 220 218 218 218 218 226 225 229 214 156 155 147 151 153 154 161 167 171 171 171 174 170 165 149 133 148 160 177 192 191 185 169 152 140 157 169 173 171 170 167 166 162 157 153 149 146 170 220 218 218 218 217 217 227 225 225 224 188 154 144 143 145 149 155 157 164 165 169 164 158 153 130 132 157 163 173 188 194 189 170 157 142 146 158 164 166 165 161 161 153 151 147 145 145 188 222 217 218 218 217 217 226 225 224 228 203 156 142 139 138 145 150 154 159 164 165 160 151 143 139 140 147 154 170 184 189 189 170 146 151 153 153 160 161 163 159 157 150 145 140 141 146 202 222 218 219 218 217 217 224 224 225 229 191 157 144 137 136 144 148 153 156 161 160 156 149 145 159 153 98 81 130 152 155 151 122 104 155 169 161 160 157 157 156 151 147 142 139 139 152 209 220 218 219 218 217 217 224 224 227 224 188 168 147 140 139 143 148 151 152 157 157 154 155 157 167 170 139 113 110 100 97 112 113 139 169 178 173 167 165 163 158 151 147 142 137 139 159 215 219 218 219 217 217 218 225 226 226 219 185 184 149 140 139 142 145 149 151 155 156 156 163 167 170 169 171 167 144 106 104 132 157 168 175 181 178 174 167 160 155 152 145 140 137 140 168 219 218 218 218 218 217 217 226 223 226 206 191 198 152 145 138 139 144 145 147 148 154 159 162 164 168 172 171 169 164 142 143 157 167 173 177 178 181 173 161 156 150 146 139 138 141 144 180 224 216 217 217 217 217 217 226 225 219 194 209 209 158 145 139 136 136 143 145 141 148 155 159 161 168 179 184 176 182 184 188 185 182 187 183 175 171 166 160 156 149 143 139 139 143 145 192 221 216 217 217 217 217 217 224 221 209 202 218 216 167 145 139 135 134 135 131 138 144 149 151 159 166 159 152 143 128 151 144 134 135 137 157 169 162 158 153 150 144 140 139 142 146 152 206 219 216 217 217 217 217 217 221 218 209 217 215 214 179 144 141 137 135 129 126 133 138 137 135 125 98 80 85 88 77 77 80 84 96 95 84 113 137 148 147 144 142 140 141 142 142 164 217 219 216 216 216 216 217 215 222 215 217 222 213 218 204 147 141 138 138 138 136 136 137 134 121 108 129 140 139 140 153 152 157 161 143 145 140 137 123 133 141 144 142 140 140 141 141 186 218 216 216 217 216 216 217 215 222 219 222 223 214 217 222 166 135 138 138 136 136 136 134 129 127 144 163 165 162 164 156 140 142 153 156 160 156 154 144 133 138 142 142 140 137 137 150 211 215 215 215 216 216 216 216 216 223 223 224 224 217 212 223 204 139 135 134 133 132 131 129 132 142 151 155 160 162 150 121 97 89 111 142 157 156 152 150 139 134 137 136 135 136 132 186 219 215 215 215 215 215 215 216 216 223 223 223 223 220 212 216 212 150 132 131 130 126 125 130 135 138 140 144 140 117 100 102 96 98 102 112 132 146 147 142 136 134 133 135 133 129 156 217 214 215 215 215 215 214 215 216 216 223 223 222 222 220 218 219 211 148 132 126 125 124 124 128 132 133 128 120 106 106 146 164 159 160 168 161 132 122 133 135 133 132 134 136 133 137 205 219 215 215 216 216 215 214 215 216 216 223 223 222 222 222 220 220 216 152 133 128 119 121 123 127 131 128 121 113 110 150 175 158 147 150 155 160 163 134 128 135 132 132 134 136 128 182 220 215 215 215 216 216 215 213 215 216 216 223 223 222 222 221 220 218 217 153 130 129 117 110 117 122 126 130 132 140 153 157 156 149 151 151 147 150 159 159 148 138 136 132 130 121 152 217 213 214 215 214 214 214 215 214 214 214 214 223 222 222 220 220 219 219 218 153 131 129 121 107 105 113 119 131 138 146 146 133 124 130 146 141 139 141 142 147 148 141 134 129 119 118 169 219 213 213 215 213 213 213 214 214 213 213 213 222 222 221 220 220 219 220 216 153 133 132 123 116 104 100 109 123 130 130 126 113 111 121 126 134 139 138 127 129 137 138 127 115 119 123 176 218 211 213 213 213 213 213 213 213 213 213 214 221 221 221 221 222 219 219 217 153 132 131 126 121 113 105 95 100 110 112 110 99 103 111 120 113 106 104 108 121 127 121 109 114 125 123 186 216 212 213 212 212 213 213 213 212 212 212 212 221 221 221 220 219 219 219 215 150 132 132 131 125 120 114 107 94 88 98 104 107 99 92 96 97 88 84 91 103 102 104 115 122 123 125 197 214 213 213 212 212 213 213 213 212 212 212 213

To train my neural network with keras, I load my data from the csv file like this : 为了用keras训练我的神经网络,我像这样从csv文件加载数据:

def Zerocenter_ZCA_whitening_Global_Contrast_Normalize(list):
Intonumpyarray = numpy.asarray(list)
data = Intonumpyarray.reshape(48,48)
data2 = ZeroCenter(data)
data3 = zca_whitening(flatten_matrix(data2)).reshape(48,48)
data4 = global_contrast_normalize(data3)
data5 = numpy.rot90(data4,3)
return data5



def load_data():
train_x = []
train_y = []
val_x =[]
val_y =[]
test_x =[]
test_y =[]

f = open('My_file.csv')
csv_f = csv.reader(f)

for row in csv_f:   
    if str(row[2]) == "Training":
       temp_list_train = []

       for pixel in row[1].split( ):
           temp_list_train.append(int(pixel))

            data = Zerocenter_ZCA_whitening_Global_Contrast_Normalize(temp_list_train)
            train_y.append(int(row[0]))
            train_x.append(data.reshape(2304).tolist())

        elif str(row[2]) == "Test" :
            temp_list_validation = []

            for pixel in row[1].split( ):
                temp_list_validation.append(int(pixel))

            data = Zerocenter_ZCA_whitening_Global_Contrast_Normalize(temp_list_validation)
            val_y.append(int(row[0]))
            val_x.append(data.reshape(2304).tolist())

        elif str(row[2]) == "Validation":
            temp_list_test = []

            for pixel in row[1].split( ):
                temp_list_test.append(int(pixel))

            data = Zerocenter_ZCA_whitening_Global_Contrast_Normalize(temp_list_test)
            test_y.append(int(row[0]))
            test_x.append(data.reshape(2304).tolist())

return train_x, train_y, val_x, val_y, test_x, test_y

This works fine. 这很好。 Now I want to train my neural network with RGB images, I have a csv file with the same format like the previous one, but the Pixels are stored in (3, 2304) (3 lines). 现在我想用RGB图像训练我的神经网络,我有一个csv文件,格式与上一个相同,但是像素存储在(3,2304)(3行)中。

How can I change the code so that I can load RGB images and train my network with them ? 如何更改代码,以便可以加载RGB图像并使用它们训练网络?

This will give you all the numbers as int in a single list 这将在一个列表中为您提供所有整数形式的int

for row in csv_f:   
    if str(row[2]) == "Training":
       temp_list_train = []

       for line in row[1].strip().split('\n'):
           temp_list_train += [int(x) for x in line.split()]

If you want to print your values as an image later on, I would suggest keeping them in format (48,48,3) and save them as an numpy.array . 如果以后要将值打印为图像,建议将其保留为格式(48,48,3)并将其另存为numpy.array You can train your Net with the image in size (1,48*48*3) or if you have many use a batch (50,1,48*48*3) if for example you have 50 images. 您可以使用图像的大小(1,48 * 48 * 3)训练网络,或者如果您使用50张图像,则使用一批(50,1,48 * 48 * 3)。 You can reshape them from (48,48,3) to (1,48*48*3) with reshape(1,-1) and then back to (48,48,3) with reshape(48,48,3) . 您可以使用reshape(1,-1)它们从(48,48,3)整形为(1,48 * 48 * 3),然后通过reshape(48,48,3)回到reshape(48,48,3)

PS. PS。 Check Tensorflow ( https://www.tensorflow.org/ ) for Neural Nets with Python 使用Python检查Tensorflow( https://www.tensorflow.org/ )中的神经网络

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM