suppressMessages(suppressWarnings(library(keras)))
suppressMessages(suppressWarnings(library(tidyverse)))
suppressMessages(suppressWarnings(require(imager)))
suppressMessages(suppressWarnings(library(MASS)))
suppressMessages(suppressWarnings(library(gridExtra)))

Como um dos objetivos principais desse relatório, é criar uma rede neural que classifique as Imagens da base Steel em duas classes com/sem defeito.

train <- readr::read_csv("/opt/datasets/steel/train.csv")
## Parsed with column specification:
## cols(
##   ImageId_ClassId = col_character(),
##   EncodedPixels = col_character()
## )
head(train)
## # A tibble: 6 x 2
##   ImageId_ClassId EncodedPixels                                            
##   <chr>           <chr>                                                    
## 1 0002cc93b.jpg_1 29102 12 29346 24 29602 24 29858 24 30114 24 30370 24 30…
## 2 0002cc93b.jpg_2 <NA>                                                     
## 3 0002cc93b.jpg_3 <NA>                                                     
## 4 0002cc93b.jpg_4 <NA>                                                     
## 5 00031f466.jpg_1 <NA>                                                     
## 6 00031f466.jpg_2 <NA>
# Lista com o local de todas as imagens
path_images <- list.files(path = "/opt/datasets/steel/train_images_dir",
                          pattern = ".jpg",recursive = TRUE,full.names = TRUE)
head(path_images)
## [1] "/opt/datasets/steel/train_images_dir/0002cc93b.jpg"
## [2] "/opt/datasets/steel/train_images_dir/00031f466.jpg"
## [3] "/opt/datasets/steel/train_images_dir/000418bfc.jpg"
## [4] "/opt/datasets/steel/train_images_dir/000789191.jpg"
## [5] "/opt/datasets/steel/train_images_dir/0007a71bf.jpg"
## [6] "/opt/datasets/steel/train_images_dir/000a4bcdd.jpg"
img_test <- imager::load.image(path_images[1])

print(img_test)
## Image. Width: 1600 pix Height: 256 pix Depth: 1 Colour channels: 3
plot(img_test,axes = T)

Identificação do erro na imagem.

train <- train %>% 
  separate(ImageId_ClassId,into = c("id","tg"),sep = "_")


# Criando os poligonos de contorno de cada erro
pixels_to_split <- train$EncodedPixels[1]
splitted_pixels <- str_split(pixels_to_split, "[:space:]") %>% # splits the EncodedPixel observation using :space: character as a pattern
  unlist() %>% # unlists/as.vector the result of str_split
  as.numeric() %>% # the valus are still characters, so we transform them into numerical values
  matrix(ncol = 2, byrow = TRUE) %>%  # create a matrix of 2 columns with the values
  as_tibble() %>% # transform the matrix into a data.frame
  rename(pixel_start = V1, run_length = V2) %>% # rename the columns
  mutate(pixel_end = pixel_start + run_length) # create a variable pixel_end
## Warning: `as_tibble.matrix()` requires a matrix with column names or a `.name_repair` argument. Using compatibility `.name_repair`.
## This warning is displayed once per session.
height <- dim(img_test)[2]
width <- dim(img_test)[1]
mask <- matrix(0, nrow = height, ncol = width)
activated_pixels <- purrr::pmap(splitted_pixels, ~ seq(..1, ..3, by=1) ) %>% unlist()
mask[activated_pixels] <- 1
plot(img_test)
t(mask) %>% as.cimg() %>% as.pixset() %>% highlight(col="red")

Atravez da linha vermelha conseguimos identificar os erros.

split_pixels <- function(id,tg,x){
  n <- length(id)
  res <- data.frame()
  for(i in 1:n){
    aux <- data.frame(id = as.character(id[i]),
                      tg = as.character(tg[i]),
                      x[i] %>% 
                        str_split("[:space:]") %>% # splits the EncodedPixel observation using :space: character as a pattern
                        unlist() %>% # unlists/as.vector the result of str_split
                        as.numeric() %>% # the valus are still characters, so we transform them into numerical values
                        matrix(ncol = 2, byrow = TRUE) %>%  # create a matrix of 2 columns with the values
                        as_tibble() %>% # transform the matrix into a data.frame
                        rename(pixel_start = V1, run_length = V2) %>% # rename the columns
                        mutate(pixel_end = pixel_start + run_length) %>% 
                        return())
    res <- rbind.data.frame(res,aux)
  }
  return(res)
}


train_masks <- train[1:2,] %>% 
  na.omit() %>% 
  pmap_dfr(.f = ~split_pixels(..1,..2,..3))
plot_img_with_masks <- function(d,Id,only_mask = FALSE){
  
  img <- imager::load.image(path_images[which(str_detect(path_images,Id))])

  Tg <- names(table(d$tg[d$id==Id]))
  
  masks <- list()
  
  dimensions <- dim(img)
  
  for(i in 1:length(Tg)){
    
    mask <- matrix(0, ncol = dimensions[1],  nrow = dimensions[2])
    
    activated_pixels <- d %>%
      dplyr::filter((id==Id)&(tg==Tg[i])) %>%
      dplyr::select(-id,-tg) %>%
      purrr::pmap(~ seq(..1, ..3, by = 1) ) %>%
      unlist()
    
    mask[activated_pixels] <- 1
    
    masks[[i]] <- mask
  } 
  
  if(only_mask==FALSE){
    plot(img,axes = FALSE)
  }
  else{
    par(mfrow = c(3,1))
    plot(img,axes = FALSE)
  
  }
  
  pal <- c("white","#33a02c","#ff7f00","#313695")
  
  for(i in 1:length(Tg)){
    
    if(only_mask==FALSE){
      t(masks[[i]]) %>%
        as.cimg() %>%
        as.pixset() %>%
        highlight(col = pal[as.numeric(Tg[i])] , lwd = 2.5)
      
      if(i==length(Tg)){
                    
  cat("-------------\n")
  cat("   Legend\n")
  cat("-------------\n")
  print(data.frame(Colour = c("White","Green","Orange","Blue"),
                   Target = c(1:4)) %>% 
          filter(Target%in%Tg))
  cat("-------------")
      }

    }
    else{
      plot(as.cimg(t(masks[[i]])),axes = FALSE)
    }
    
   }

  
}

train_masks %>% 
  plot_img_with_masks(Id = "41411378e.jpg")

## -------------
##    Legend
## -------------
##   Colour Target
## 1  White      1
## -------------
train_masks %>% 
  plot_img_with_masks(Id = "41411378e.jpg",only_mask = TRUE)

# Total de defeitos gerais
train %>% 
  mutate(tem_erro = is.na(EncodedPixels)==FALSE) %>%
  group_by(id) %>% 
  mutate(total = sum(n()/4)) %>% 
  ungroup() %>% 
  group_by(tg) %>% 
  summarise(n = sum(tem_erro),
            p = n/sum(total)) %>% 
  arrange(-p) # 41% dos defeitos é do tipo 3 XD
## # A tibble: 4 x 3
##   tg        n      p
##   <chr> <int>  <dbl>
## 1 3      5150 0.410 
## 2 1       897 0.0714
## 3 4       801 0.0637
## 4 2       247 0.0197

Probabilidadede defeitos.

transformando os dados de imagem para matriz.

directory <- "/opt/datasets/steel/train_images_dir/"
id_images <- stringr::str_remove(path_images,pattern = "/opt/datasets/steel/train_images_dir/")

set.seed(111)
images_sample <- sample(1:length(id_images),size = 500,replace = FALSE)

img_train <- image_load(paste0(directory,id_images[1]))
img_array <- keras::image_to_array(img_train)

images_train <- array(NA,dim = c(500,256,1600,1))

for(i in 1:500){
  cat("\n Processing",i,"of",500)
  img_train <-image_load(paste0(directory,id_images[i]))
  img_array <- keras::image_to_array(img_train)
  images_train[i,,,] <- img_array[,,1]
}
## 
##  Processing 1 of 500
##  Processing 2 of 500
##  Processing 3 of 500
##  Processing 4 of 500
##  Processing 5 of 500
##  Processing 6 of 500
##  Processing 7 of 500
##  Processing 8 of 500
##  Processing 9 of 500
##  Processing 10 of 500
##  Processing 11 of 500
##  Processing 12 of 500
##  Processing 13 of 500
##  Processing 14 of 500
##  Processing 15 of 500
##  Processing 16 of 500
##  Processing 17 of 500
##  Processing 18 of 500
##  Processing 19 of 500
##  Processing 20 of 500
##  Processing 21 of 500
##  Processing 22 of 500
##  Processing 23 of 500
##  Processing 24 of 500
##  Processing 25 of 500
##  Processing 26 of 500
##  Processing 27 of 500
##  Processing 28 of 500
##  Processing 29 of 500
##  Processing 30 of 500
##  Processing 31 of 500
##  Processing 32 of 500
##  Processing 33 of 500
##  Processing 34 of 500
##  Processing 35 of 500
##  Processing 36 of 500
##  Processing 37 of 500
##  Processing 38 of 500
##  Processing 39 of 500
##  Processing 40 of 500
##  Processing 41 of 500
##  Processing 42 of 500
##  Processing 43 of 500
##  Processing 44 of 500
##  Processing 45 of 500
##  Processing 46 of 500
##  Processing 47 of 500
##  Processing 48 of 500
##  Processing 49 of 500
##  Processing 50 of 500
##  Processing 51 of 500
##  Processing 52 of 500
##  Processing 53 of 500
##  Processing 54 of 500
##  Processing 55 of 500
##  Processing 56 of 500
##  Processing 57 of 500
##  Processing 58 of 500
##  Processing 59 of 500
##  Processing 60 of 500
##  Processing 61 of 500
##  Processing 62 of 500
##  Processing 63 of 500
##  Processing 64 of 500
##  Processing 65 of 500
##  Processing 66 of 500
##  Processing 67 of 500
##  Processing 68 of 500
##  Processing 69 of 500
##  Processing 70 of 500
##  Processing 71 of 500
##  Processing 72 of 500
##  Processing 73 of 500
##  Processing 74 of 500
##  Processing 75 of 500
##  Processing 76 of 500
##  Processing 77 of 500
##  Processing 78 of 500
##  Processing 79 of 500
##  Processing 80 of 500
##  Processing 81 of 500
##  Processing 82 of 500
##  Processing 83 of 500
##  Processing 84 of 500
##  Processing 85 of 500
##  Processing 86 of 500
##  Processing 87 of 500
##  Processing 88 of 500
##  Processing 89 of 500
##  Processing 90 of 500
##  Processing 91 of 500
##  Processing 92 of 500
##  Processing 93 of 500
##  Processing 94 of 500
##  Processing 95 of 500
##  Processing 96 of 500
##  Processing 97 of 500
##  Processing 98 of 500
##  Processing 99 of 500
##  Processing 100 of 500
##  Processing 101 of 500
##  Processing 102 of 500
##  Processing 103 of 500
##  Processing 104 of 500
##  Processing 105 of 500
##  Processing 106 of 500
##  Processing 107 of 500
##  Processing 108 of 500
##  Processing 109 of 500
##  Processing 110 of 500
##  Processing 111 of 500
##  Processing 112 of 500
##  Processing 113 of 500
##  Processing 114 of 500
##  Processing 115 of 500
##  Processing 116 of 500
##  Processing 117 of 500
##  Processing 118 of 500
##  Processing 119 of 500
##  Processing 120 of 500
##  Processing 121 of 500
##  Processing 122 of 500
##  Processing 123 of 500
##  Processing 124 of 500
##  Processing 125 of 500
##  Processing 126 of 500
##  Processing 127 of 500
##  Processing 128 of 500
##  Processing 129 of 500
##  Processing 130 of 500
##  Processing 131 of 500
##  Processing 132 of 500
##  Processing 133 of 500
##  Processing 134 of 500
##  Processing 135 of 500
##  Processing 136 of 500
##  Processing 137 of 500
##  Processing 138 of 500
##  Processing 139 of 500
##  Processing 140 of 500
##  Processing 141 of 500
##  Processing 142 of 500
##  Processing 143 of 500
##  Processing 144 of 500
##  Processing 145 of 500
##  Processing 146 of 500
##  Processing 147 of 500
##  Processing 148 of 500
##  Processing 149 of 500
##  Processing 150 of 500
##  Processing 151 of 500
##  Processing 152 of 500
##  Processing 153 of 500
##  Processing 154 of 500
##  Processing 155 of 500
##  Processing 156 of 500
##  Processing 157 of 500
##  Processing 158 of 500
##  Processing 159 of 500
##  Processing 160 of 500
##  Processing 161 of 500
##  Processing 162 of 500
##  Processing 163 of 500
##  Processing 164 of 500
##  Processing 165 of 500
##  Processing 166 of 500
##  Processing 167 of 500
##  Processing 168 of 500
##  Processing 169 of 500
##  Processing 170 of 500
##  Processing 171 of 500
##  Processing 172 of 500
##  Processing 173 of 500
##  Processing 174 of 500
##  Processing 175 of 500
##  Processing 176 of 500
##  Processing 177 of 500
##  Processing 178 of 500
##  Processing 179 of 500
##  Processing 180 of 500
##  Processing 181 of 500
##  Processing 182 of 500
##  Processing 183 of 500
##  Processing 184 of 500
##  Processing 185 of 500
##  Processing 186 of 500
##  Processing 187 of 500
##  Processing 188 of 500
##  Processing 189 of 500
##  Processing 190 of 500
##  Processing 191 of 500
##  Processing 192 of 500
##  Processing 193 of 500
##  Processing 194 of 500
##  Processing 195 of 500
##  Processing 196 of 500
##  Processing 197 of 500
##  Processing 198 of 500
##  Processing 199 of 500
##  Processing 200 of 500
##  Processing 201 of 500
##  Processing 202 of 500
##  Processing 203 of 500
##  Processing 204 of 500
##  Processing 205 of 500
##  Processing 206 of 500
##  Processing 207 of 500
##  Processing 208 of 500
##  Processing 209 of 500
##  Processing 210 of 500
##  Processing 211 of 500
##  Processing 212 of 500
##  Processing 213 of 500
##  Processing 214 of 500
##  Processing 215 of 500
##  Processing 216 of 500
##  Processing 217 of 500
##  Processing 218 of 500
##  Processing 219 of 500
##  Processing 220 of 500
##  Processing 221 of 500
##  Processing 222 of 500
##  Processing 223 of 500
##  Processing 224 of 500
##  Processing 225 of 500
##  Processing 226 of 500
##  Processing 227 of 500
##  Processing 228 of 500
##  Processing 229 of 500
##  Processing 230 of 500
##  Processing 231 of 500
##  Processing 232 of 500
##  Processing 233 of 500
##  Processing 234 of 500
##  Processing 235 of 500
##  Processing 236 of 500
##  Processing 237 of 500
##  Processing 238 of 500
##  Processing 239 of 500
##  Processing 240 of 500
##  Processing 241 of 500
##  Processing 242 of 500
##  Processing 243 of 500
##  Processing 244 of 500
##  Processing 245 of 500
##  Processing 246 of 500
##  Processing 247 of 500
##  Processing 248 of 500
##  Processing 249 of 500
##  Processing 250 of 500
##  Processing 251 of 500
##  Processing 252 of 500
##  Processing 253 of 500
##  Processing 254 of 500
##  Processing 255 of 500
##  Processing 256 of 500
##  Processing 257 of 500
##  Processing 258 of 500
##  Processing 259 of 500
##  Processing 260 of 500
##  Processing 261 of 500
##  Processing 262 of 500
##  Processing 263 of 500
##  Processing 264 of 500
##  Processing 265 of 500
##  Processing 266 of 500
##  Processing 267 of 500
##  Processing 268 of 500
##  Processing 269 of 500
##  Processing 270 of 500
##  Processing 271 of 500
##  Processing 272 of 500
##  Processing 273 of 500
##  Processing 274 of 500
##  Processing 275 of 500
##  Processing 276 of 500
##  Processing 277 of 500
##  Processing 278 of 500
##  Processing 279 of 500
##  Processing 280 of 500
##  Processing 281 of 500
##  Processing 282 of 500
##  Processing 283 of 500
##  Processing 284 of 500
##  Processing 285 of 500
##  Processing 286 of 500
##  Processing 287 of 500
##  Processing 288 of 500
##  Processing 289 of 500
##  Processing 290 of 500
##  Processing 291 of 500
##  Processing 292 of 500
##  Processing 293 of 500
##  Processing 294 of 500
##  Processing 295 of 500
##  Processing 296 of 500
##  Processing 297 of 500
##  Processing 298 of 500
##  Processing 299 of 500
##  Processing 300 of 500
##  Processing 301 of 500
##  Processing 302 of 500
##  Processing 303 of 500
##  Processing 304 of 500
##  Processing 305 of 500
##  Processing 306 of 500
##  Processing 307 of 500
##  Processing 308 of 500
##  Processing 309 of 500
##  Processing 310 of 500
##  Processing 311 of 500
##  Processing 312 of 500
##  Processing 313 of 500
##  Processing 314 of 500
##  Processing 315 of 500
##  Processing 316 of 500
##  Processing 317 of 500
##  Processing 318 of 500
##  Processing 319 of 500
##  Processing 320 of 500
##  Processing 321 of 500
##  Processing 322 of 500
##  Processing 323 of 500
##  Processing 324 of 500
##  Processing 325 of 500
##  Processing 326 of 500
##  Processing 327 of 500
##  Processing 328 of 500
##  Processing 329 of 500
##  Processing 330 of 500
##  Processing 331 of 500
##  Processing 332 of 500
##  Processing 333 of 500
##  Processing 334 of 500
##  Processing 335 of 500
##  Processing 336 of 500
##  Processing 337 of 500
##  Processing 338 of 500
##  Processing 339 of 500
##  Processing 340 of 500
##  Processing 341 of 500
##  Processing 342 of 500
##  Processing 343 of 500
##  Processing 344 of 500
##  Processing 345 of 500
##  Processing 346 of 500
##  Processing 347 of 500
##  Processing 348 of 500
##  Processing 349 of 500
##  Processing 350 of 500
##  Processing 351 of 500
##  Processing 352 of 500
##  Processing 353 of 500
##  Processing 354 of 500
##  Processing 355 of 500
##  Processing 356 of 500
##  Processing 357 of 500
##  Processing 358 of 500
##  Processing 359 of 500
##  Processing 360 of 500
##  Processing 361 of 500
##  Processing 362 of 500
##  Processing 363 of 500
##  Processing 364 of 500
##  Processing 365 of 500
##  Processing 366 of 500
##  Processing 367 of 500
##  Processing 368 of 500
##  Processing 369 of 500
##  Processing 370 of 500
##  Processing 371 of 500
##  Processing 372 of 500
##  Processing 373 of 500
##  Processing 374 of 500
##  Processing 375 of 500
##  Processing 376 of 500
##  Processing 377 of 500
##  Processing 378 of 500
##  Processing 379 of 500
##  Processing 380 of 500
##  Processing 381 of 500
##  Processing 382 of 500
##  Processing 383 of 500
##  Processing 384 of 500
##  Processing 385 of 500
##  Processing 386 of 500
##  Processing 387 of 500
##  Processing 388 of 500
##  Processing 389 of 500
##  Processing 390 of 500
##  Processing 391 of 500
##  Processing 392 of 500
##  Processing 393 of 500
##  Processing 394 of 500
##  Processing 395 of 500
##  Processing 396 of 500
##  Processing 397 of 500
##  Processing 398 of 500
##  Processing 399 of 500
##  Processing 400 of 500
##  Processing 401 of 500
##  Processing 402 of 500
##  Processing 403 of 500
##  Processing 404 of 500
##  Processing 405 of 500
##  Processing 406 of 500
##  Processing 407 of 500
##  Processing 408 of 500
##  Processing 409 of 500
##  Processing 410 of 500
##  Processing 411 of 500
##  Processing 412 of 500
##  Processing 413 of 500
##  Processing 414 of 500
##  Processing 415 of 500
##  Processing 416 of 500
##  Processing 417 of 500
##  Processing 418 of 500
##  Processing 419 of 500
##  Processing 420 of 500
##  Processing 421 of 500
##  Processing 422 of 500
##  Processing 423 of 500
##  Processing 424 of 500
##  Processing 425 of 500
##  Processing 426 of 500
##  Processing 427 of 500
##  Processing 428 of 500
##  Processing 429 of 500
##  Processing 430 of 500
##  Processing 431 of 500
##  Processing 432 of 500
##  Processing 433 of 500
##  Processing 434 of 500
##  Processing 435 of 500
##  Processing 436 of 500
##  Processing 437 of 500
##  Processing 438 of 500
##  Processing 439 of 500
##  Processing 440 of 500
##  Processing 441 of 500
##  Processing 442 of 500
##  Processing 443 of 500
##  Processing 444 of 500
##  Processing 445 of 500
##  Processing 446 of 500
##  Processing 447 of 500
##  Processing 448 of 500
##  Processing 449 of 500
##  Processing 450 of 500
##  Processing 451 of 500
##  Processing 452 of 500
##  Processing 453 of 500
##  Processing 454 of 500
##  Processing 455 of 500
##  Processing 456 of 500
##  Processing 457 of 500
##  Processing 458 of 500
##  Processing 459 of 500
##  Processing 460 of 500
##  Processing 461 of 500
##  Processing 462 of 500
##  Processing 463 of 500
##  Processing 464 of 500
##  Processing 465 of 500
##  Processing 466 of 500
##  Processing 467 of 500
##  Processing 468 of 500
##  Processing 469 of 500
##  Processing 470 of 500
##  Processing 471 of 500
##  Processing 472 of 500
##  Processing 473 of 500
##  Processing 474 of 500
##  Processing 475 of 500
##  Processing 476 of 500
##  Processing 477 of 500
##  Processing 478 of 500
##  Processing 479 of 500
##  Processing 480 of 500
##  Processing 481 of 500
##  Processing 482 of 500
##  Processing 483 of 500
##  Processing 484 of 500
##  Processing 485 of 500
##  Processing 486 of 500
##  Processing 487 of 500
##  Processing 488 of 500
##  Processing 489 of 500
##  Processing 490 of 500
##  Processing 491 of 500
##  Processing 492 of 500
##  Processing 493 of 500
##  Processing 494 of 500
##  Processing 495 of 500
##  Processing 496 of 500
##  Processing 497 of 500
##  Processing 498 of 500
##  Processing 499 of 500
##  Processing 500 of 500
images_train_model <- images_train[1:350,,,]
images_test_model <- images_train[351:500,,,]
id_images_id <- data.frame(id_images) %>% 
  mutate(i = 1:length(id_images))

id_images_id <- id_images_id[id_images_id$i[images_sample],]

targets <- train %>% 
  group_by(id) %>% 
  summarise(y = if_else(sum(is.na(EncodedPixels)==TRUE)==4,0,1)) %>% 
  ungroup() %>% 
  filter(id %in% id_images_id$id_images)

# Nao estao na mesma ordem
targets$id == id_images_id$id_images
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [100] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [111] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [122] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [144] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [155] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [166] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [177] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [188] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [199] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [210] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [221] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [232] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [243] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [254] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [265] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [276] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [287] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [298] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [309] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [320] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [331] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [342] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [353] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [364] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [375] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [386] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [397] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [408] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [419] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [430] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [441] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [452] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [463] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [474] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [485] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [496] FALSE FALSE FALSE FALSE FALSE
y_train_model <- tibble(id = id_images[images_sample[1:1400]]) %>% 
  mutate(order = row_number()) %>% 
  left_join(targets,by = "id") %>% 
  arrange(order) %>% 
  dplyr::select(y) %>% 
  unlist(use.names = FALSE) %>% 
  as.matrix()

y_test_model <- tibble(id = id_images[images_sample[1401:2000]]) %>% 
  mutate(order = row_number()) %>% 
  left_join(targets,by = "id") %>% 
  arrange(order) %>% 
  dplyr::select(y) %>% 
  unlist(use.names = FALSE) %>% 
  as.matrix()

Não é possivel trenar o modelo no rmarkdow, pois esta ultrapassando o limite da memoria. Então os codigos seram so exibidos. x_train_cnn <- array_reshape(images_train_model, c(350, 256, 1600, 1))/255 # Normalizando

x_test_cnn <- array_reshape(images_test_model, c(150, 256, 1600, 1))/255 # Normalizando

model_cnn <- keras_model_sequential() %>% layer_conv_2d( filters = 32, kernel_size = c(3, 3), activation = ‘relu’, input_shape = c(256,1600,1)) %>% layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = ‘relu’) %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>% layer_dropout(rate = 0.25) %>% layer_flatten() %>% # these are the embeddings (activations) we are going to visualize layer_dense(units = 16, activation = ‘relu’, name = ‘features3’) %>% layer_dropout(rate = 0.1) %>% layer_dense(units = 2, activation = ‘softmax’)

model_cnn %>% compile( loss = “categorical_crossentropy”, optimizer = optimizer_adadelta(), metrics = c(‘accuracy’) )

summary(model_cnn)

testando o modelo.

evaluate(model_cnn,x_test_cnn,to_categorical(y_test_model))

DoLciNópOlis prefixáSSeIs