Wolfram Language

Redes neurais

Classificação de imagens fora do núcleo

Treine uma rede para distinguir os algarismos 1 de 2, e carregar apenas pequenos lotes de imagens do disco para a memória um de cada vez.

Baixe um conjunto de imagens e descompacte-os.

In[1]:=
Click for copyable input
zip = URLDownload["https://wolfr.am/ebyHmnkR", "characters12.zip"]; dir = CreateDirectory[]; ExtractArchive[First @ zip, dir] // Length
Out[1]=

Obtenha a rota dos arquivo de imagem e obtenha as classes dos nomes das pastas.

In[2]:=
Click for copyable input
loadFiles[dir_] := Map[File[#] -> FileNameTake[#, {-2}] &, FileNames["*.jpg", dir, Infinity]]; trainingData = loadFiles[FileNameJoin[{dir, "characters", "train"}]]; testData = loadFiles[FileNameJoin[{dir, "characters", "test"}]];

O dados de treinamento correspondem a uma lista de regras de objetos de File a classes.

In[3]:=
Click for copyable input
RandomSample[trainingData, 3]
Out[3]=

Defina uma rede simples.

In[4]:=
Click for copyable input
net = NetChain[ {FlattenLayer[], DotPlusLayer[2], SoftmaxLayer[]}, "Output" -> NetDecoder[{"Class", {"1", "2"}}], "Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}] ]
Out[4]=

Treine a rede.

In[5]:=
Click for copyable input
trained = NetTrain[net, trainingData, ValidationSet -> testData, MaxTrainingRounds -> 600, BatchSize -> 64]
Out[6]=

Classifique imagens diretamente da rota de arquivos.

In[7]:=
Click for copyable input
ims = Keys@RandomSample[testData, 3] trained /@ ims
Out[7]=
Out[7]=

Ou avalie diretamente em uma imagem e obtenha as probabilidades de classificação.

In[8]:=
Click for copyable input
trained[\!\(\* GraphicsBox[ TagBox[RasterBox[CompressedData[" 1:eJztVUtrGlEUNj6iiY9UySJIN125zUq6kEhEIg3oIqULlwlNQ0GmqIVqRCQG NZJV8Ee4yUbBXVYJ5Ff4A4yiece308859TBMHkwX3fVADtc7537nO8982Pmx 9U2r0WjiJqit7V/rsdh28vM7/PgixL/vCbtfPwk/d/d2Yx93dLh8/+dvehZf kclkAv3w8CD/ORqNoB8fH9ns/v4eejAYvIYjF3pOcnV1RYfr6+vxeIwDdLfb ZRu58RvS6/XkPPlMJPv9PpEE8s3NjRpAEqLE+KenpxsbG3q9fprkeNxoNObz eTIAshpAhElQCLxerx8eHmokmZubgzaZppWzWq2xWEwei0oJBAKEBoZLS0s4 2O12zUxOTk5gMxwO1QM2m83FxUW8NZvNYOVyuUQpjbVaDTcGgwGfkKK7uzs1 aNQksKdIc7lcu90WZ9VBoYknMsxpVyPUkxNJ6IaTDMzNzU1gZrNZ9YB4y50M hKenJ/6E7CWTSQAuLCxEIhHcqIxdDs5Qt7e30KANepSTYrHYaDRUQnHLKeaO RsYoCXjCi8ohIkHyuffAls/lcplaK5VK0U2r1VIDCATFHBF5XDqdTur8v02j KEUN5MvLy4ODg/39/ePjYxDT6XTURegu2CB2efneFvRnKBTCW61WSyBIIB0s Fgs0eok6Fsy52eTPOS6SQqGAVzabjTYGJojQ5ufn2QWNPNZIp9N5TgleuHZI kd/vZ3pra2ulUsnr9cqRM5kMDb5ekouLC0XpeRPiHjoYDGKKER2MBUE4Ojpa XV3F8+XlZXjxeDzn5+coWaVSCYfD5AL3itjl2YA9L5xEIoHYcaAdgqh9Pp8i Y7z6XiwHEcYyBB+YgSrWDoGvrKy43e5oNIoJAkMUGquegsU0pdPps7MzbjZF MrEfkHB5/hE+qIKwggPZwwXP2vN/Hxw78oCMORwOAKL01WqVvlIHonzoWLgW Z1MAcKC9uOsQjih1OFlSKsgRDTtpdk0ueFTJy3/5d/IbNwt7sQ== "], {{0, 28}, { 28, 0}}, {0, 255}, ColorFunction->RGBColor], BoxForm`ImageTag["Byte", ColorSpace -> "RGB", Interleaving -> True], Selectable->False], DefaultBaseStyle->"ImageGraphics", ImageSize->Automatic, ImageSizeRaw->{28, 28}, PlotRange->{{0, 28}, {0, 28}}]\), "Probabilities"]
Out[8]=

Exemplos Relacionados

de en es fr ja ko ru zh