This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from colorizers import * | |
| import coremltools as ct | |
| import torch | |
| siggraph17_model = siggraph17(pretrained=True).eval() # ➊ | |
| example_input = torch.rand(1, 1, 256, 256) | |
| traced_model = torch.jit.trace(siggraph17_model, example_input) # ➋ | |
| coreml_model = ct.convert( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def colorize(image_name): | |
| image = img_to_array(load_img(image_name)) | |
| image = np.array(image, dtype=np.float32) | |
| X = rgb2lab(1.0/255*image)[:, :, 0] | |
| X = X.reshape(1, 400, 400, 1) | |
| model = ct.models.MLModel('model_alpha.mlmodel') | |
| input = np.zeros((1, 400, 400)) | |
| input[0] = X[0][:, :, 0] | |
| output = model.predict({'input_1': X})["Identity"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def convert_model(model): | |
| model.save('tf_keras_model') | |
| mlmodel = ct.convert('tf_keras_model') | |
| mlmodel.save("model_alpha.mlmodel") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def train_model(image_name, model): | |
| image = img_to_array(load_img(image_name)) | |
| image = np.array(image, dtype=np.float32) | |
| X = rgb2lab(1.0/255*image)[:, :, 0] | |
| Y = rgb2lab(1.0/255*image)[:, :, 1:] | |
| Y /= 128 | |
| X = X.reshape(1, 400, 400, 1) | |
| Y = Y.reshape(1, 400, 400, 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def create_model(): | |
| model = Sequential() | |
| model.add(InputLayer(input_shape=(400, 400, 1))) | |
| model.add(Conv2D(8, (3, 3), activation='relu', padding='same', strides=2)) | |
| model.add(Conv2D(8, (3, 3), activation='relu', padding='same')) | |
| model.add(Conv2D(16, (3, 3), activation='relu', padding='same')) | |
| model.add(Conv2D(16, (3, 3), activation='relu', padding='same', strides=2)) | |
| model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) | |
| model.add(Conv2D(32, (3, 3), activation='relu', padding='same', strides=2)) | |
| model.add(UpSampling2D((2, 2))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func fadeInPlayerAnimator(with duration: TimeInterval) -> UIViewPropertyAnimator { | |
| let animator = UIViewPropertyAnimator(duration: duration, curve: .easeIn) | |
| addKeyframeAnimation(to: animator, withRelativeStartTime: 0.0, relativeDuration: 0.5) { | |
| self.updatePlayer(with: self.state) | |
| } | |
| animator.scrubsLinearly = false | |
| return animator | |
| } | |
| private func fadeOutMiniPlayerAnimator(with duration: TimeInterval) -> UIViewPropertyAnimator { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func openPlayerAnimator(with duration: TimeInterval) -> UIViewPropertyAnimator { | |
| let animator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1.0) | |
| addAnimation(to: animator, animations: { | |
| self.updatePlayerContainer(with: self.state) | |
| self.updateTabBar(with: self.state) | |
| }) | |
| return animator | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func createTransitionAnimators(with duration: TimeInterval) -> [UIViewPropertyAnimator] { | |
| switch state { | |
| case .open: | |
| return [ | |
| openPlayerAnimator(with: duration), | |
| fadeInPlayerAnimator(with: duration), | |
| fadeOutMiniPlayerAnimator(with: duration) | |
| ] | |
| case .closed: | |
| return [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func continueInteractiveTransition(cancel: Bool) { | |
| if cancel { | |
| runningAnimators.reverse() | |
| state = !state | |
| } | |
| runningAnimators.continueAnimations() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func updateInteractiveTransition(distanceTraveled: CGFloat) { | |
| var fraction = distanceTraveled / totalAnimationDistance | |
| if state == .open { fraction *= -1 } | |
| runningAnimators.fractionComplete = fraction | |
| } |