์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
- XCTest
- designpattern
- ๋ฐฑ์ค
- combine
- MVVM
- Kuring
- raywenderlich
- reactorkit
- realm
- Swfit
- SwiftUI
- ํจ์คํธ์บ ํผ์ค
- TCA
- SnapKit
- RxSwift
- BOJ
- visionOS
- Flutter
- Lv2
- ios
- UIKit
- tableView
- arkit
- rxcocoa
- BFS
- CollectionView
- Xcode
- ํ๋ก๊ทธ๋๋จธ์ค
- swift
- node.js
- Today
- Total
lgvv98
ch19 ๐ค CreateML ์ฌ์ฉ ๋ฐ ์ฝ๋๋ฆฌ๋ทฐ ๋ณธ๋ฌธ
ch19 ๐ค CreateML ์ฌ์ฉ ๋ฐ ์ฝ๋๋ฆฌ๋ทฐ
๐ฅ ์บ๋ฟ๋งจ 2021. 7. 19. 15:39โ ์ด๋ฒ ์๊ฐ์๋ ๋ง์ง๋ง CreateML ๋ง๋๋ ๋ฒ๊ณผ ์ฝ๋๋ฆฌ๋ทฐ๋ฅผ ํจ๊ป ๋ณด๋๋ก ํ์
์ฐ์ Xcode์์ CreateML์ ๋๋ฌ์ ์ด์ด๋ณด๋๋ก ํ์.
๋๋ฅด๋ฉด ์ด๋ ๊ฒ ์ด๋ฆฌ๊ฒ ๋๋๋ฐ ๋ด๊ฐ ์ด๋ฏธ ๋ชจ๋ธ ์์ค์ ๋ง๋ค์ด ๋์๋๋ฐ, ์๋ก ์ด๋ฉด ์ด๋ฐ์์ผ๋ก ๋ง๋ค์ด์ง๊ฒ ๋๋ค.
์ดํ ํธ๋ ์ด๋ ๋ฐ์ดํฐ ๋ถ๋ถ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ฉด ๋๋ค.
ํธ๋ ์ด๋ ๋ฐ์ดํฐ์๋ pets-100์ ๋ฃ์ด์ฃผ๊ณ , Testing Data์ชฝ์๋ pets-testing ๋ฐ์ดํฐ ๋ฃ์ด์ค๋ค.
pets-prctice๋ ๋์ค์ ์ฐ๋ฆฌ๊ฐ ํ์ต์ ๋ง์น ํ์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
โ๏ธpets-100 ํด๋๋ฅผ ๋ฃ์ด์ผ์ง ํ์์ dog๋ง ๋ฃ์ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ ์ฃผ์ํ๋๋ก ํ๋ค.
ํธ๋ ์ด๋์ ์ํค๋ฉด ๋ผ!!! ์์ฃผ ๊ฐ๋จํ๋ค.
โ ํ๋ผ๋ฏธํฐ (Maximum Iteratoins) : ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณตํ๋ ๊ฑด๋ฐ, ์๋ฅผ ๋ค์ด์ ๊ตฌ๊ธ์ ๊ฒฝ์ฐ์๋ ๋ฐ์ดํฐ์ ์๊ฐ ์์ฒญ ๋ง์์ ์ด ๊ฐ์ด ์์๋ ๊ด์ฐฎ๋ค. ๊ทธ๋ฌ๋ ์คํํธ์ ์ ๊ฒฝ์ฐ ๋ฐ์ดํฐ์ ์๊ฐ ์์ฒญ ๋ง์ด ์๊ณ ํ์ ์ ์ธ๋ฐ ํ์ ์ ๋ฐ์ดํฐ์์ ์ต์ ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฝ๊ธฐ ์ํด์ ๋ฐ๋ณต์ ์ผ๋ก ํ์ต์ํจ๋ค.
โ๏ธ์ฃผ์ํ ์ฌํญ์ผ๋ก๋ ์ค๋ฒํผํ ์ด๋ผ๊ณ ์คํ ํ๊ฒฝ์์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋๋ฌด ๋ง์ด ๋ฐ๋ณต์ํค๋ฉด ์ค์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์์ ์, ์ฌ๋ฐ๋ฅด๊ฒ ํ์ ํ์ง ๋ชปํ๋ ์ค์๋ฅผ ๋ฒํ๋๋ฐ, ์คํ๋ ค ๋ฐ๋ณต์ ๋ง์ด ์์ผฐ์์๋ ์ ํ๋๊ฐ ๋ฎ์์ง๊ฒ ๋๋ค. ์ฐ๋ฆฌ๋ ์ด๊ฒ์ ์ค๋ฒํผํ ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค.
โ Augmentations
Augment Data๋ ์ด๋ฏธ์ง์ ๋ ธ์ด์ฆ๋ฅผ ์ฃผ๊ณ ๋ ํ์ ๋ฑ ๋ค์ํ ์กฐ๊ฑด์ ์ค์ ํ ์คํ ์ํค๋๋ฐ, ์ฐ๋ฆฌ๋ ์ด๊ฒ์ ํตํด ์ ํ์ฑ์ ๋ ๋์ผ ์๋ ์๋ค.
๊ฒฐ๊ณผ๊ฐ ์ด๋ ๊ฒ ๋จ๋๋ฐ, ์ฌ๊ธฐ ๋ณด์ฌ์ง๋ ์ผ์ด์ค๋ ๋ชจ๋ 100%๋ผ๊ณ ๋์ค๋ ์ค์ ๋ก๋ ์์ ํ 100%๊ฐ ์๋ ์๋ ์๋ค.
Precision : true๋ผ๊ณ ํ ๊ฒ์ค์ ์ ๋ง true๋ ๋ชํ๋ก์ผ?
Recall : ์ ์ฒด์ค์ ํ๋ฆฌ์์
์ํค๋ํผ์ ๋ฌธ์๋ฅด ๋ณด๋ฉด ์์ฃผ ์ ๋์์๋ค!!
๊ฒฐ๊ตญ์ ์์น๊ฐ ๋ ๋ค ๋์ผ๋ฉด ์ข๋ค๋ ๊ฒ
์ฌ๋ฌ๊ฐ์ ํด๋์ค ํ์ผ์ ๋ง๋ค์ด์ ํ ์คํธ ํด๋ณด๊ณ ๊ทธ ์ค์์ ๊ฐ์ฅ ๋ฐธ๋ฅ๊ฐ ๋์ ๊ฒ์ ์ฌ์ฉํ๋ค.
Roc curve(๊ฐ๋ ) : ์ด๊ฒ๋ ๊ตฌ๊ธ์ ์น๋ฉด ๋์ค๋๋ฐ, ์์ญ์ด ๋์ด์ผ์ง ์ ํ๋๊ฐ ๋๋ค.
CoreML์์ ์ ๊ณตํด์ฃผ๋ ๊ฒ์๋ ํ๊ณ๊ฐ ์๊ธฐ ๋๋ฌธ์ AI ์์ง๋์ด๋ ๋ค๋ฅธ ๊ฒ๋ค๋ ์ธ ์ ์๋ค.
Turi Create : ์ ํ ๊ณต์๋ฌธ์์์ ์ ๊ณตํด์ฃผ๋ ํด์ด๋ค.
tensorflow : ์๋ ์ค์ํํธ๋ก๋ณํํด์ ์ฌ์ฉํ ์ ์๋ค
Pytorch : ์๋ ์์ฒด์ ์ผ๋ก iOS๋ฅผ ์ง์ํด์ฃผ๋๋ฐ, ์ด๊ฒ๋ ์ข๋ค.
โ ์ฝ๋ ๋ฆฌ๋ทฐ!
โ ์ ํ์์ ์ ๊ณตํด์ฃผ๋ ํ์ผ์ธ๋ฐ, ์ด๊ฑธ ๊ฐ๊ณ ์ค์ตํ ์์ ์ด๋ค.
DogCatClassifier๋ ์ฐ๋ฆฌ๊ฐ ๋ง๋ mlํ์ผ์ ํธ๋ ์ด๋์ ๋ง์น ๊ฒ์ ๋ฃ๋๋ค๊ณ ์๊ฐํ์.
โ ImageClassificationView ๋ฅผ ํ๋ฒ ๋ณผ๊น?
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
View controller for selecting images and applying Vision + Core ML processing.
*/
import UIKit
import CoreML
import Vision
import ImageIO
class ImageClassificationViewController: UIViewController {
// MARK: - IBOutlets
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var cameraButton: UIBarButtonItem!
@IBOutlet weak var classificationLabel: UILabel!
// MARK: - Image Classification
/// - Tag: MLModelSetup
lazy var classificationRequest: VNCoreMLRequest = {
do {
/*
Use the Swift class `MobileNet` Core ML generates from the model.
To use a different Core ML classifier model, add it to the project
and replace `MobileNet` with that model's generated Swift class.
*/
let model = try VNCoreMLModel(for: DogCatClassifier().model) // ๋น์ ์ฝ์ดML๋ชจ๋ธ๋ก ๋ง๋ค๊ธฐ
let request = VNCoreMLRequest(model: model, completionHandler: { [weak self] request, error in
self?.processClassifications(for: request, error: error) // ๋ชจ๋ธํํ
์ด๋ฏธ์ง ๋ถ๋ฌ์จ๊ฑฐ ์์ฒญ์ ํด์ผํด
// ๋ฆฌํ์คํธ๊ฐ ๋ณด๋ด์ง๊ณ ๋์ ๊ทธ์ ํด๋นํ๋ ํธ๋ค๋ก๋ ์ด๋ ๊ฒ ํ๋ค. CoreML๋ชจ๋ธ ์
์
ํด์ฃผ๊ธฐ
})
request.imageCropAndScaleOption = .centerCrop // ๋จธ์ ๋ชจ๋ธ ์ด๋ฏธ์ง๋ณด๋ค ๋น์จ์ด ๋ ํด์๋ ์์์๋ ์๊ณ ๋ฑ ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ์ ๋ง๋ค๋ ์กฐ๊ฑด์ ์ค์ ํ๋๋ฐ, ์ด๊ฑฐ๋ ํฌ๊ฒ 3๊ฐ์ง๊ฐ ์๋ค.
// ์ผํฐํฌ๋กญ, ์ค์ผ์ผ ํํด์ ์์ ๋ฑ ๋ง๊ฒํด์ ํ์ , ํ์ ์์ผ์ ๊ฝ ์ฑ์์ ํ์
// ์ ๋ต์ด ์๊ธด ๋ณด๋ค๋ ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ ๋ง๋ค๋ ์ฌ์ฉํ ์กฐ๊ฑด์ ์ฌ์ฉํ๋๋ฐ, ์ฃผ๋ก ์ผํฐํฌ๋กญ ๋ง์ด ์ฌ์ฉํ๋ค๊ณ ํ๋ค.
return request
} catch {
fatalError("Failed to load Vision ML model: \(error)")
}
}()
/// - Tag: PerformRequests
func updateClassifications(for image: UIImage) {
classificationLabel.text = "Classifying..."
let orientation = CGImagePropertyOrientation(image.imageOrientation)
guard let ciImage = CIImage(image: image) else { fatalError("Unable to create \(CIImage.self) from \(image).") }
DispatchQueue.global(qos: .userInitiated).async {
let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation)
do {
try handler.perform([self.classificationRequest])
} catch {
/*
This handler catches general image processing errors. The `classificationRequest`'s
completion handler `processClassifications(_:error:)` catches errors specific
to processing that request.
*/
print("Failed to perform classification.\n\(error.localizedDescription)")
}
}
}
/// Updates the UI with the results of the classification.
/// - Tag: ProcessClassifications
// ์์ฒญํ ๋ ๋ถ๋ฆฌ๋ ํจ์ ์ฆ, ๊ฐค๋ฌ๋ฆฌ์์ ์ฌ์ง ์ ํํ์๋, ๊ทธ๊ฑธ ๋ถ์ํด์ ํ
์คํธ๋ฅผ ๋ด๋ณด๋ด๋ ๊ฒ ๊น์ง๊ฐ ์ฌ๊ธฐ์ ํ๋ ๊ณผ์
// ์ํ๋๋ ์๊ฐ์ ์ด๋ฏธ์ง๊ฐ ์ ํํ์ ๋๋ผ๋ ๊ฒ์ ์์ง๋ง๊ธฐ!! ๊ทธ๋ ์ค์ํ๊ฒ ๋ญ๋๋ฉด ๋น์ ์ ์๋ ํ๋ ์์ค์ ์๋ ๋ฆฌํ์คํธ ํธ๋ค๋ฌ๋ฅผ ๋ง๋ค์ด์ ์ด๊ฑธ ๋ง๋ค๊ณ ์ค์ ๋ก ์ํ์ ์์ผ์ฃผ๋ ๋
์!
func processClassifications(for request: VNRequest, error: Error?) {
DispatchQueue.main.async {
guard let results = request.results else {
self.classificationLabel.text = "Unable to classify image.\n\(error!.localizedDescription)"
return
}
// The `results` will always be `VNClassificationObservation`s, as specified by the Core ML model in this project.
let classifications = results as! [VNClassificationObservation]
// ์ฌ๊ธฐ ์๋๊ฐ ์ค์ ๊ฒฐ๊ณผ๋ฌผ์ด ์๋ ๋ถ๋ถ
// ์ฐ๋ฆฌ๋ ํด๋์ค๊ฐ 2๊ฐ์ง๋ง MobileNet์ ๊ฒฝ์ฐ์๋ ํด๋์ค๊ฐ ๋ฌด๋ ค 1000๊ฐ๊ฐ ๋๋ค.
// mlํ์ผ์์ ํด๋์ค์ ๋ํ ๊ฒฐ๊ณผ๋ฌผ์ ๊ฐ์ ธ์์ description์ ์ด๋ค.
if classifications.isEmpty {
self.classificationLabel.text = "Nothing recognized."
} else {
// Display top classifications ranked by confidence in the UI.
let topClassifications = classifications.prefix(2)
let descriptions = topClassifications.map { classification in
// Formats the classification for display; e.g. "(0.37) cliff, drop, drop-off".
return String(format: " (%.2f) %@", classification.confidence, classification.identifier) // ๊ฒฐ๊ณผ ์์ ํด๋์ค์ ์ ํ๋์ ๋ํ ์ ๋ณด๊ฐ ์๋๋ฐ ๊ทธ๊ฒ์ ์ฐ๋ฆฌ๋ ์คํธ๋ง์ผ๋ก ๋ง๋ค์ด์ค ๊ฒ์ด๋ค.
}
self.classificationLabel.text = "Classification:\n" + descriptions.joined(separator: "\n")
}
}
}
// MARK: - Photo Actions
@IBAction func takePicture() {
// Show options for the source picker only if the camera is available.
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
presentPhotoPicker(sourceType: .photoLibrary)
return
}
let photoSourcePicker = UIAlertController()
let takePhoto = UIAlertAction(title: "Take Photo", style: .default) { [unowned self] _ in
self.presentPhotoPicker(sourceType: .camera)
}
let choosePhoto = UIAlertAction(title: "Choose Photo", style: .default) { [unowned self] _ in
self.presentPhotoPicker(sourceType: .photoLibrary)
}
photoSourcePicker.addAction(takePhoto)
photoSourcePicker.addAction(choosePhoto)
photoSourcePicker.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(photoSourcePicker, animated: true)
}
func presentPhotoPicker(sourceType: UIImagePickerControllerSourceType) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = sourceType
present(picker, animated: true)
}
}
extension ImageClassificationViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: - Handling Image Picker Selection
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
picker.dismiss(animated: true)
// We always expect `imagePickerController(:didFinishPickingMediaWithInfo:)` to supply the original image.
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
updateClassifications(for: image)
}
}
์ฝ๋๋ก ์์ฑํด ๋์๋ค!! ์ฃผ์์ ๋ฌ์ ๋์์ผ๋ ์ฝ์ด๋ณด๋๋ก ํ๋ค.
'โ ๏ธ deprecated โ ๏ธ > ํจ์บ (์ฌ์ธ์)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ch19 ๐ค CoreML (0) | 2021.07.19 |
---|---|
๐ธ ch18 FullScreen ์นด๋ฉ๋ผ ์ฑ ์ฝ๋๋ฆฌ๋ทฐ (0) | 2021.07.06 |
๐ธ ch 18 AVFoundation ์นดํ ๊ณ ๋ฆฌ ๋ณ๋ก ํ๊ตฌ (0) | 2021.07.05 |
๐ฌ ch17 Netflix ํ์ฅ์ฑ ์ฝ๋๋ฆฌ๋ทฐ(firebase, kingfisher) + ch15 (0) | 2021.07.05 |
ch17 ๋์ ScrollView Guide! ์ํ์ข์ฐ!! (0) | 2021.06.29 |